내 응용 프로그램에서 전역 변수를 선언하는 클래스를 하나 만들었습니다.언제 ARC에서 변수를 공개하거나 없앨까요?
.H 파일
@interface GlobalVariables : NSObject
@property (nonatomic, strong) NSString *strTemp1;
@property (nonatomic, strong) NSArray *arrTemp1;
+ (GlobalVariables *)sharedInstance ;
하는 .m 파일 일부 클래스 이제
#import "GlobalVariables.h"
@implementation GlobalVariables
+ (GlobalVariables *)sharedInstance
{
static GlobalVariables *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [self new];
// Do any other initialisation stuff here
NSLog(@"alloc and intialize");
});
return sharedInstance;
}
, 나는이 변수의 값을 설정합니다.
[[GlobalVariables sharedInstance] setStrTemp1:@"Temp1"];
[[GlobalVariables sharedInstance] setArrTemp1:[NSArray arrayWithObjects:@"Name",@"Address",@"Contact NO", nil]];
또한 다른 클래스에서이 값을 가져옵니다.
NSLog(@"Get string value : %@", [[GlobalVariables sharedInstance] strTemp1]);
NSLog(@"Get arr value : %@", [[GlobalVariables sharedInstance] arrTemp1]);
이제 내 질문은이 변수가 릴리스 또는 nil 일 때입니까?
프로젝트에서 ARC를 사용하고 있습니다.
호가 자동으로 출시된다는 것을 알고 있지만 언제, 어떻게 사용합니까?
ARC를 사용하는 경우 실제로 메모리를 해제하는시기와 방법을 신경 쓰지 않아야합니다. – FreeAsInBeer
before iOS 5 ARC mens에 대한 개념이 없으므로 작성한 모든 객체를 해제해야합니다 ...... 거기에 아무 것도 잊어 버리면 경고 메시지가 나타납니다. 따라서 iOS 5 이후에는 ARC (Automatic Reference counting) ... 객체를 할당하고 아무 문제없이 잊어 버리십시오. –