不能分配给具有 const 限定类型的变量
cannot assign to variable with const-qualified type
我正在尝试如下动态更改 SWSegueFrontIdentifier 的值,因为前视图是动态的。以下是我的代码
if ([prefs boolForKey:@"isDashboardAvailable"])
{
SWSegueFrontIdentifier = @"sw_front";
} else {
SWSegueFrontIdentifier = @"sw_second_front";
}
但我收到以下错误
Cannot assign to variable 'SWSegueFrontIdentifier' with
const-qualified type 'NSString *const __strong'
为什么会出现此错误?我怎样才能更改 SWSegueFrontIdentifier 值?
你的问题它自己给你答案。你试图改变 constant
的值,这是不可能的,你不能改变常量的值。如果你想改变 SWSegueFrontIdentifier
的值声明是变量而不是像这样的常量
NSString *SWSegueFrontIdentifier = @"";
if ([prefs boolForKey:@"isDashboardAvailable"])
{
SWSegueFrontIdentifier = @"sw_front";
}
else {
SWSegueFrontIdentifier = @"sw_second_front";
}
既然你想让它成为动态的前视图控制器,试试下面的..
创建另一个 const 变量如下
NSString * const SWSegueSecondFrontIdentifier = @"sw_second_front";
然后在loadStoryboardControllers
方法中实现如下代码
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs boolForKey:@"isDashboardAvailable"])
{
[self performSegueWithIdentifier:SWSegueSecondFrontIdentifier sender:nil];
} else {
[self performSegueWithIdentifier:SWSegueFrontIdentifier sender:nil];
}
如有困难请返回。
我正在尝试如下动态更改 SWSegueFrontIdentifier 的值,因为前视图是动态的。以下是我的代码
if ([prefs boolForKey:@"isDashboardAvailable"])
{
SWSegueFrontIdentifier = @"sw_front";
} else {
SWSegueFrontIdentifier = @"sw_second_front";
}
但我收到以下错误
Cannot assign to variable 'SWSegueFrontIdentifier' with const-qualified type 'NSString *const __strong'
为什么会出现此错误?我怎样才能更改 SWSegueFrontIdentifier 值?
你的问题它自己给你答案。你试图改变 constant
的值,这是不可能的,你不能改变常量的值。如果你想改变 SWSegueFrontIdentifier
的值声明是变量而不是像这样的常量
NSString *SWSegueFrontIdentifier = @"";
if ([prefs boolForKey:@"isDashboardAvailable"])
{
SWSegueFrontIdentifier = @"sw_front";
}
else {
SWSegueFrontIdentifier = @"sw_second_front";
}
既然你想让它成为动态的前视图控制器,试试下面的..
创建另一个 const 变量如下
NSString * const SWSegueSecondFrontIdentifier = @"sw_second_front";
然后在loadStoryboardControllers
方法中实现如下代码
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs boolForKey:@"isDashboardAvailable"])
{
[self performSegueWithIdentifier:SWSegueSecondFrontIdentifier sender:nil];
} else {
[self performSegueWithIdentifier:SWSegueFrontIdentifier sender:nil];
}
如有困难请返回。