* 확실히 중단해야합니다 ... 원인은 간단합니다 - 배열이 할당되지 않았습니다 ... 도움을 주셔서 감사합니다. 그 당혹스러운 실수 때문에, 나는 그것을 삭제하기 위해 내 게시물에 플래그를 지정했습니다. 사용자에게 유용하지는 않습니다.) *목표 C - 샘플 싱글 톤 구현
iOS에서 싱글 톤 클래스를 만들려고했지만 실수를하고있는 것 같습니다. 코드 (더 ARC가 필요 없음) :
#import "PeopleDatabase.h"
#import "Person.h"
#import <Foundation/Foundation.h>
@interface PeopleDatabase : NSObject{objetive
NSMutableArray* _arrayOfPeople;
}
+(PeopleDatabase *) getInstance;
@property (nonatomic, retain) NSMutableArray* arrayOfPeople;
@end
-
@implementation PeopleDatabase
@synthesize arrayOfPeople = _arrayOfPeople;
static PeopleDatabase* instance = nil;
-(id)init{
if(self = [super init]) {
Person* person = [[[Person alloc] initWithName:@"John" sname:@"Derovsky" descr:@"Some kind of description" iconName:@"johnphoto.png" title:Prof] retain];
[_arrayOfPeople addObject:person];
NSLog(@"array count = %d", [_arrayOfPeople count]); // <== array count = 0
[person release];
}
return self;
}
+(PeopleDatabase *)getInstance {
@synchronized(self)
{
if (instance == nil)
NSLog(@"initializing");
instance = [[[self alloc] init] retain];
NSLog(@"Address: %p", instance);
}
return(instance);
}
-(void)dealloc {
[instance release];
[super dealloc];
}
@end
여기처럼의 getInstance를 호출 :
PeopleDatabase *database = [PeopleDatabase getInstance];
NSLog(@"Adress 2: %p", database);
주소 2 값의 getInstance에서와 같은 값.
정확하게 "주소 2는 getInstance와 동일한 값을가집니다."는 의미입니까? ? – Petar
그것은 동일한 주소이어야한다. 그것의 동일한 목표부터? getInstance에서 반환 한 객체는 데이터베이스가 할당 된 객체입니다. – Fonix
[이 답변보기] (http://stackoverflow.com/a/145395/730701). 이 기능 +에서 – Adam