저는 ARC가 아닌 프로젝트를 진행하고 있습니다. 프로젝트에는 전역 함수 클래스처럼 사용되는 단일 클래스가 있습니다.싱글 톤은 ARC 및 비 ARC가있는 프로젝트에서 작동합니까?
모두 정상적으로 작동합니다. 다음과 같은 문제를 제외하고 :
- 추가 ARC와 클래스 싱글 톤 클래스가 ARC 기반 클래스에서 액세스 할 때,
- 이 아마 싱글 클래스와 더 방출되어 처음 작동
- 싱글 톤 클래스에 대한 호출은 "메시지가 할당되지 않은 인스턴스로 전송 됨"메시지와 함께 응용 프로그램을 무효화합니다.
내가 알 수 있듯이 ARC 가능 클래스는 싱글 톤 객체를 방출하는 것으로 생각됩니다.
어떻게이 문제를 해결할 수 있습니까?
편집 : 싱글 클래스 초기화 GlobalFunctions.m
#import "GlobalFunctions.h"
#import <CoreData/CoreData.h>
#import "UIImage+Tint.h"
#import "Reachability.h"
#if !TARGET_IPHONE_SIMULATOR
#define Type @"Device"
#else
#define Type @"Simulator"
#endif
@implementation GlobalFunctions
#pragma mark {Synthesize}
@synthesize firstLaunch=_firstLaunch;
@synthesize context = _context;
#pragma mark {Initializer}
static GlobalFunctions *sharedGlobalFunctions=nil;
- (UIColor *)UIColorFromRGB:(NSInteger)red:(NSInteger)green:(NSInteger) blue {
CGFloat nRed=red/255.0;
CGFloat nBlue=green/255.0;
CGFloat nGreen=blue/255.0;
return [[[UIColor alloc]initWithRed:nRed green:nBlue blue:nGreen alpha:1] autorelease];
}
#pragma mark {Class Intialization}
+(GlobalFunctions *)sharedGlobalFunctions{
if(sharedGlobalFunctions==nil){
// sharedGlobalFunctions=[[super allocWithZone:NULL] init];
sharedGlobalFunctions=[[GlobalFunctions alloc] init]; //Stack Overflow recommendation, does'nt work
// Custom initialization
/*
Variable Initialization and checks
*/
[email protected]"YES";
id appDelegate=(id)[[UIApplication sharedApplication] delegate];
sharedGlobalFunctions.context=[appDelegate managedObjectContext];
}
return sharedGlobalFunctions;
}
-(id)copyWithZone:(NSZone *)zone{
return self;
}
-(id)retain{
return self;
}
-(NSUInteger) retainCount{
return NSUIntegerMax;
}
-(void) dealloc{
[super dealloc];
[_context release];
}
@end
GlobalFunctions.h
#import <Foundation/Foundation.h>
@interface GlobalFunctions : NSObject<UIApplicationDelegate>{
NSString *firstLaunch;
}
+(GlobalFunctions *)sharedGlobalFunctions; //Shared Object
#pragma mark {Function Declarations}
-(UIColor *)UIColorFromRGB:(NSInteger)red:(NSInteger)green:(NSInteger) blue; // Convert color to RGB
#pragma mark {Database Objects}
@property (nonatomic,retain) NSManagedObjectContext *context;
@end
편집 : Anshu이 제안 [초기화 [GlobalFunctions의 ALLOC]]를 사용하여 시도
. 하지만 여전히 앱이 "할당 취소 된 인스턴스로 전송되었습니다"라는 메시지와 충돌합니다.
싱글 톤을 어떻게 구현 했습니까? 객체에 대한 강력한 참조가있는 한 ARC가 무작위로 객체를 릴리스해야하는 이유는 없습니다. 싱글 톤 구현에 문제가있는 것처럼 들립니다. –
안녕하세요 Anshu, 내 싱글 톤 클래스의 이니셜 라이저를 추가했습니다. 맞지 않다고 확신합니까? 그렇지? – Veeru
여기서'allocWithZone :'을 사용하고 싶지 않습니다. [ARC release notes] (http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html)에서는 어쨌든 메모리 영역을 더 이상 사용할 수 없다고 말합니다. '[[GlobalFunctions alloc] init]'만 시도해보십시오. –