gpt4 book ai didi

ios - 无法更新单例类中的属性

转载 作者:行者123 更新时间:2023-11-28 19:04:33 24 4
gpt4 key购买 nike

我有一个像这样声明的单例类:

+(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;
}

在这个类中,我声明了一个公共(public)属性:

@property (assign, nonatomic) int countryId;

现在当我像这样从另一个类设置这个属性时:

[[BBDataStore sharedDataStore]setCountryId:1];

我的自定义 setter 在单例类中运行:

-(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,并且从不更改其值。我在这里做错了什么?

最佳答案

您的自定义 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;
}
}

关于ios - 无法更新单例类中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21701046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com