3

iOS에서 인스턴스 생성시 CSV 파일을 구문 분석 한 다음 결과를 보유하는 싱글 톤 개체가 있습니다. 이 객체를 보편적으로 액세스 가능하게 만들고 싶습니다. 앱을 종료 할 때까지 메모리에서 해제하지 않기를 바랍니다. 수동 유지를 수행 할 수 없도록 ARC를 실행 중입니다.앱이 종료 될 때까지 변수를 메모리에 유지하는 방법

내가 ARC와 함께 작동하도록 할 수있는 방법이 있습니까?

헤더 파일 :

@interface Foo : NSObject 

+ (Foo *)sharedInstance; 

@end 

과하는 .m에 : 간단한 싱글은 .H에 ... 뭔가 같은 것

#import "ParserStore.h" 
#import "RCParserObject.h" 
#import "RCDetailItem.h" 

static ParserStore *defaultStore; 

@implementation ParserStore 

@synthesize item, data, parsedData, fields, open; 

# pragma mark - 
# pragma mark Singleton Methods 

+ (ParserStore *) defaultStore 
{ 
    if (!defaultStore) 
    { 
     defaultStore = [[super allocWithZone:NULL] init]; 
    } 
    return defaultStore; 
} 

+ (id) allocWithZone:(NSZone *)zone 
{ 
    return [self defaultStore]; 
} 

- (id) init 
{ 
    NSLog(@"Running init on parser store"); 

    if (defaultStore) 
    { 
     NSLog(@"Self data count is %d, right before return", self.parsedData.count); 
     return defaultStore; 
    } 

    // NSLog(@"This better only happen once"); 
    self = [super init]; 
    [self setParsedData:[[NSMutableArray alloc] init]]; 
    [self parseCSVFile:@"ContentNoPathFileExt2ASCII"]; 
    [self categorizeData]; 
    // NSLog(@"Self data count is %d when first created", self.parsedData.count); 

    return self; 
} 

    @property (atomic, retain) RCParserObject *item; 
    @property (atomic, retain) NSMutableArray *data; 
    @property (atomic, retain) NSMutableArray *parsedData; 
    @property (atomic) int fields; 
    @property (atomic) bool open; 

    + (ParserStore *) defaultStore; 

    - (void) parseCSVFile:(NSString*)file; 
    - (void) categorizeData; 

    @end 
+0

그것에 대한 참조. 싱글 톤 .m에서 정적 변수에 저장된 싱글 톤의 인스턴스입니까? –

+0

나는 그것을하지 않았다, 당신은 저에게 구문을 말할 수 있는가? –

+0

오, 신경 쓰지 마라. 나는 그것을했다. 그러나 나는 그것을 무가치하게 만들었다. 그렇지 않습니까? –

답변

4
+(MySingleton *)singleton { 
    static dispatch_once_t pred; 
    static MySingleton *shared = nil; 
    dispatch_once(&pred, ^{ 
     shared = [[MySingleton alloc] init]; 
     shared.someVar = someValue; // if you want to initialize an ivar 
    }); 
    return shared; 
} 

:

NSLog(@"%@",[MySingleton singleton].someVar); 

참고하여 iOS 앱은 이미 어디서든 액세스 할 수있는 싱글을 가지고 :

항상 있기 때문에 싱글은 ARC에 의해 해제되지해야
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 
0

#import <Foundation/Foundation.h> 
#import "CHCSV.h" 
#import "RCParserObject.h" 


@interface ParserStore : NSObject <CHCSVParserDelegate> 
{ 
    // CSV Variables 
    RCParserObject *item; 
    NSMutableArray *data; 
    NSMutableArray *parsedData; 
    int fields; 
    bool open; 
} 

@property (atomic, retain) RCParserObject *item; 
@property (atomic, retain) NSMutableArray *data; 
@property (atomic, retain) NSMutableArray *parsedData; 
@property (atomic) int fields; 
@property (atomic) bool open; 

+ (ParserStore *) defaultStore; 

- (void) parseCSVFile:(NSString*)file; 
- (void) categorizeData; 

구현 파일 :

static Foo *_foo = nil; 

@implementation Foo 

+ (Foo *)sharedInstance { 
    if (!_foo) 
    _foo = [[Foo alloc] init]; 
    return _foo; 
} 

@end 
어디에서
+0

위의 코드로이 작업을 수행 한 것으로 보입니다. 작동하지 않는 것 같습니다 ... –

+0

흠 .. 이상하게 생겼습니다. 나는 이런 종류의 작업을 자주하고 있으며 ARC에서 아무런 문제가 없었습니다. –

+0

기본적으로, 나는 거기에 가변 배열을 가지고 있으며, 때때로 그 배열의 수는 0으로 떨어지며, 그것은 발생하지 않아야한다. –