iOS什么时候使用全局变量 self
1个回答
展开全部
ios 单例全局变量写法
interface MySingleton : NSObject
{
⇒① NSString *testGlobal;
}
+ (MySingleton *)sharedSingleton;
⇒②@property (nonxxxx,retain) NSString *testGlobal;
@end
@implementation MySingleton
⇒③@synthesize testGlobal;
+ (MySingleton *)sharedSingleton
{
static MySingleton *sharedSingleton;
@synchronized(self)
{
if (!sharedSingleton)
sharedSingleton = [[MySingleton alloc] init];
return sharedSingleton;
}
}
interface MySingleton : NSObject
{
⇒① NSString *testGlobal;
}
+ (MySingleton *)sharedSingleton;
⇒②@property (nonxxxx,retain) NSString *testGlobal;
@end
@implementation MySingleton
⇒③@synthesize testGlobal;
+ (MySingleton *)sharedSingleton
{
static MySingleton *sharedSingleton;
@synchronized(self)
{
if (!sharedSingleton)
sharedSingleton = [[MySingleton alloc] init];
return sharedSingleton;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询