Ios 无法更新singleton类中的属性
我有一个单例类,声明如下:Ios 无法更新singleton类中的属性,ios,iphone,objective-c,singleton,Ios,Iphone,Objective C,Singleton,我有一个单例类,声明如下: +(instancetype)mySharedClass { static BBDataStore *sharedBBDataStore = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedBBDataStore = [[self alloc]initWithDataExpiry:DATA_EXPIRY_TIME];
+(instancetype)mySharedClass
{
static BBDataStore *sharedBBDataStore = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedBBDataStore = [[self alloc]initWithDataExpiry:DATA_EXPIRY_TIME];
});
return sharedBBDataStore;
}
-(instancetype)initWithDataExpiry: (int) dataExpiry
{
if (self = [super initWithDataExpiry:dataExpiry])
{
self.categoryWebServicePool = [[NSMutableDictionary alloc]init];
self.userProfileWebServicePool = [[NSMutableDictionary alloc]init];
}
return self;
}
[[BBDataStore sharedDataStore]setCountryId:1];
在这个类中,我有一个公共属性声明为:
@property (assign, nonatomic) int countryId;
现在,当我从另一个类设置此属性时,如下所示:
+(instancetype)mySharedClass
{
static BBDataStore *sharedBBDataStore = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedBBDataStore = [[self alloc]initWithDataExpiry:DATA_EXPIRY_TIME];
});
return sharedBBDataStore;
}
-(instancetype)initWithDataExpiry: (int) dataExpiry
{
if (self = [super initWithDataExpiry:dataExpiry])
{
self.categoryWebServicePool = [[NSMutableDictionary alloc]init];
self.userProfileWebServicePool = [[NSMutableDictionary alloc]init];
}
return self;
}
[[BBDataStore sharedDataStore]setCountryId:1];
我的自定义setter在singleton类中运行:
-(void)setCountryId:(int)countryId
{
switch (countryId)
{
case RE_INDEX:
self.serverString = RE_SERVER_STRING;
self.authId = BB_AUTH_SA_ID;
break;
case KE_INDEX:
self.serverString = KE_SERVER_STRING;
self.authId = BB_AUTH_KENYA_ID;
break;
}
}
但是,self.countryId始终保持为0且从不更改其值。我在这里做错了什么?问题在于这一行:
[[BBDataStore sharedDataStore]setCountryId:1];
你应该像这样使用:
[[BBDataStore mySharedClass] setCountryId:1];
您永远不会直接访问singleton对象
另外,您没有在自定义setter中设置countryId属性。问题在于这一行:
[[BBDataStore sharedDataStore]setCountryId:1];
你应该像这样使用:
[[BBDataStore mySharedClass] setCountryId:1];
您永远不会直接访问singleton对象
另外,您没有在自定义setter中设置
countryId
属性。您的自定义setter方法实际上不会更改基础ivar的值。这应该行得通
-(void)setCountryId:(int)countryId
{
_countryId = countryId;
switch (countryId)
{
case RE_INDEX:
self.serverString = RE_SERVER_STRING;
self.authId = BB_AUTH_SA_ID;
break;
case KE_INDEX:
self.serverString = KE_SERVER_STRING;
self.authId = BB_AUTH_KENYA_ID;
break;
}
}
您的自定义setter方法实际上不会更改基础ivar的值。这应该行得通
-(void)setCountryId:(int)countryId
{
_countryId = countryId;
switch (countryId)
{
case RE_INDEX:
self.serverString = RE_SERVER_STRING;
self.authId = BB_AUTH_SA_ID;
break;
case KE_INDEX:
self.serverString = KE_SERVER_STRING;
self.authId = BB_AUTH_KENYA_ID;
break;
}
}
您需要更新countryId。在setter中调用
\u countryId=countryId
应该可以实现更新countryId所需的技巧。在setter中调用\u countryId=countryId
应该可以做到这一点。真不敢相信我居然错过了。iOS开发人员还是新手。谢谢。这很有效。真不敢相信我居然错过了。iOS开发人员还是新手。谢谢您解决了这个问题。非常感谢你。我只是假设setter设置了属性。没有;我不知道我必须直接设置ivar。哦,不用担心。或者,您可以在感兴趣的属性上使用键值观察(KVO),这样您就不必处理它(或在setter中处理副作用)。这仍然是iOS游戏的新手。因此,我们将阅读KVO,看看它是否更适合这个问题。解决了这个问题。非常感谢你。我只是假设setter设置了属性。没有;我不知道我必须直接设置ivar。哦,不用担心。或者,您可以在感兴趣的属性上使用键值观察(KVO),这样您就不必处理它(或在setter中处理副作用)。这仍然是iOS游戏的新手。因此,我们将仔细阅读KVO,看看它是否更适合这个问题。