【问题标题】:cannot assign to variable with const-qualified type不能分配给具有 const 限定类型的变量
【发布时间】:2016-11-10 22:30:20
【问题描述】:

我尝试如下动态更改 SWSegueFrontIdentifier 的值,因为前视图是动态的。以下是我的代码

if ([prefs boolForKey:@"isDashboardAvailable"])
{
    SWSegueFrontIdentifier = @"sw_front";          
} else {
    SWSegueFrontIdentifier = @"sw_second_front";
}

但我收到以下错误

无法分配给变量“SWSegueFrontIdentifier” const 限定类型 'NSString *const __strong'

为什么会出现这个错误?如何更改 SWSegueFrontIdentifier 值?

【问题讨论】:

  • 为什么不在 loadStoryBoardControllers 方法中采用这个逻辑?

标签: ios objective-c swrevealviewcontroller


【解决方案1】:

你的问题它自己给你答案。您正在尝试更改constant 的值,这是不可能的,您无法更改常量的值。如果要更改SWSegueFrontIdentifier 的值,则声明为变量而不是像这样的常量

NSString *SWSegueFrontIdentifier = @""; 
if ([prefs boolForKey:@"isDashboardAvailable"])
{
    SWSegueFrontIdentifier = @"sw_front";
} 
else {
    SWSegueFrontIdentifier = @"sw_second_front";
}

【讨论】:

    【解决方案2】:

    既然您希望它是动态前视图控制器,请尝试以下操作..

    创建另一个常量变量如下

    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];
         }
    

    如果遇到任何困难,请返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 2021-08-22
      • 2021-06-17
      • 1970-01-01
      相关资源
      最近更新 更多