diff --git a/ContaPessoas.xcodeproj/project.pbxproj b/ContaPessoas.xcodeproj/project.pbxproj index 9ce30d1..35bcc83 100644 --- a/ContaPessoas.xcodeproj/project.pbxproj +++ b/ContaPessoas.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ 27A199D11A94B501008DC684 /* boy.png in Resources */ = {isa = PBXBuildFile; fileRef = 27A199D01A94B501008DC684 /* boy.png */; }; 27A199D31A94D1FA008DC684 /* Contador.m in Sources */ = {isa = PBXBuildFile; fileRef = 27A199D21A94D1FA008DC684 /* Contador.m */; }; 27A199D61A94D821008DC684 /* ContadorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 27A199D51A94D821008DC684 /* ContadorTests.m */; }; + E37FFAB51A9658C60061174F /* Singleton.m in Sources */ = {isa = PBXBuildFile; fileRef = E37FFAB41A9658C60061174F /* Singleton.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -52,6 +53,8 @@ 27A199D21A94D1FA008DC684 /* Contador.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Contador.m; sourceTree = ""; }; 27A199D41A94D22B008DC684 /* Contador.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Contador.h; sourceTree = ""; }; 27A199D51A94D821008DC684 /* ContadorTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ContadorTests.m; sourceTree = ""; }; + E37FFAB31A9658C60061174F /* Singleton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Singleton.h; sourceTree = ""; }; + E37FFAB41A9658C60061174F /* Singleton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Singleton.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -116,6 +119,8 @@ children = ( 27A199A41A94B2A8008DC684 /* Info.plist */, 27A199A51A94B2A8008DC684 /* main.m */, + E37FFAB31A9658C60061174F /* Singleton.h */, + E37FFAB41A9658C60061174F /* Singleton.m */, ); name = "Supporting Files"; sourceTree = ""; @@ -243,6 +248,7 @@ 27A199AF1A94B2A8008DC684 /* SecondViewController.m in Sources */, 27A199A91A94B2A8008DC684 /* AppDelegate.m in Sources */, 27A199AC1A94B2A8008DC684 /* FirstViewController.m in Sources */, + E37FFAB51A9658C60061174F /* Singleton.m in Sources */, 27A199D31A94D1FA008DC684 /* Contador.m in Sources */, 27A199A61A94B2A8008DC684 /* main.m in Sources */, ); @@ -438,6 +444,7 @@ 27A199C81A94B2A8008DC684 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 27A199C91A94B2A8008DC684 /* Build configuration list for PBXNativeTarget "ContaPessoasTests" */ = { isa = XCConfigurationList; @@ -446,6 +453,7 @@ 27A199CB1A94B2A8008DC684 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 18b7c16..7954d93 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -8,11 +8,14 @@ @interface Contador : NSObject + - (void)maisUmCueca; - (void)maisUmaGata; -(int)getBoys; -(int)getGirls; +-(int)getTotal; ++ (Contador *) instancia; @end diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index fa64ad8..bf25678 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -13,13 +13,16 @@ @implementation Contador { int boy; int girl; + int total; + } - +static Contador *_instancia; -(id)init { self = [super init]; if (self) { boy = 0; girl = 0; + _instancia = nil; } return self; } @@ -39,6 +42,18 @@ -(int)getGirls { return girl; } +-(int)getTotal +{ + return total = [self getBoys] + [self getGirls]; +} + ++ (Contador *) instancia { + + if (_instancia == nil) { + _instancia = [[Contador alloc] init]; + } + return _instancia; +} @end diff --git a/ContaPessoas/FirstViewController.h b/ContaPessoas/FirstViewController.h index 59c8301..9627fb7 100644 --- a/ContaPessoas/FirstViewController.h +++ b/ContaPessoas/FirstViewController.h @@ -12,6 +12,6 @@ - (IBAction)clickBoy:(id)sender; - (IBAction)clickGirl:(id)sender; - +// teste @end diff --git a/ContaPessoas/FirstViewController.m b/ContaPessoas/FirstViewController.m index fc70dd5..128e95a 100644 --- a/ContaPessoas/FirstViewController.m +++ b/ContaPessoas/FirstViewController.m @@ -19,7 +19,7 @@ @implementation FirstViewController { - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador instancia]; // Do any additional setup after loading the view, typically from a nib. } diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index a2a66f5..6594e01 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -19,11 +19,10 @@ @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + //contador = [[Contador alloc] init]; + contador = [Contador instancia]; } - - - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. @@ -32,7 +31,7 @@ - (void)didReceiveMemoryWarning { - (IBAction)click:(id)sender { _totalBoys.text = [NSString stringWithFormat: @"%d", [contador getBoys]]; _totalGirls.text = [NSString stringWithFormat: @"%d", [contador getGirls]]; - _total.text = [NSString stringWithFormat:@"%d", [contador getGirls] + [contador getBoys] ]; + _total.text = [NSString stringWithFormat:@"%d", [contador getTotal]]; } diff --git a/ContaPessoas/Singleton.h b/ContaPessoas/Singleton.h new file mode 100644 index 0000000..4132d5c --- /dev/null +++ b/ContaPessoas/Singleton.h @@ -0,0 +1,14 @@ +// +// Singleton.h +// ContaPessoas +// +// Created by Rafael Hieda on 19/02/15. +// Copyright (c) 2015 Vinicius Miana. All rights reserved. +// + +#import + +@interface Singleton : NSObject +{} ++ (Singleton *) instancia; +@end diff --git a/ContaPessoas/Singleton.m b/ContaPessoas/Singleton.m new file mode 100644 index 0000000..46cb2ea --- /dev/null +++ b/ContaPessoas/Singleton.m @@ -0,0 +1,21 @@ +// +// Singleton.m +// ContaPessoas +// +// Created by Rafael Hieda on 19/02/15. +// Copyright (c) 2015 Vinicius Miana. All rights reserved. +// + +#import "Singleton.h" + +@implementation Singleton +static Singleton *_instancia = nil; + ++ (Singleton *) instancia { + + if (_instancia == nil) { + _instancia = [[Singleton alloc] init]; + } + return _instancia; +} +@end diff --git a/ContaPessoas/Singleton/Singleton/main.m b/ContaPessoas/Singleton/Singleton/main.m new file mode 100644 index 0000000..fcee04a --- /dev/null +++ b/ContaPessoas/Singleton/Singleton/main.m @@ -0,0 +1,17 @@ +// +// main.m +// Singleton +// +// Created by Rafael Hieda on 19/02/15. +// Copyright (c) 2015 Rafael Hieda. All rights reserved. +// + +#import + +int main(int argc, const char * argv[]) { + @autoreleasepool { + // insert code here... + NSLog(@"Hello, World!"); + } + return 0; +} diff --git a/ContaPessoasTests/ContaPessoasTests.m b/ContaPessoasTests/ContaPessoasTests.m index 579bb3f..fc227ee 100644 --- a/ContaPessoasTests/ContaPessoasTests.m +++ b/ContaPessoasTests/ContaPessoasTests.m @@ -9,6 +9,7 @@ #import #import #import "Contador.h" +#import "Singleton.h" @interface ContaPessoasTests : XCTestCase @@ -16,11 +17,19 @@ @interface ContaPessoasTests : XCTestCase @implementation ContaPessoasTests +-(void)CriaSingleton +{ + Singleton *novoSingleton = [[Singleton alloc]init]; + Contador *aux = [[Contador alloc]init]; + aux = (Contador *) novoSingleton; + +} - (void)testContaMeninos { Contador *c = [[Contador alloc] init]; [c maisUmCueca]; XCTAssert(([c getBoys] == 1), @"Pass"); + XCTAssert(([c getGirls] == 0), @"Pass"); } - (void)testContaMeninas {