4
使用常量NSString作为NSUserDefaults的键
我正在使用NSUSerDefaults存储用户首选项。我记得在某处读过,将键设置为常量是一个好主意-我同意。以下代码是我目前拥有的: [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:polygon.numberOfSides] forKey:@"polygonNumberOfSides"]; 我尝试将其更改为: @implementation Controller NSString const *kPolygonNumberOfSides = @"polygonNumberOfSides"; -(void)savePolygonInfo { [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:polygon.numberOfSides] forKey:kPolygonNumberOfSides]; } 在执行此操作时,它会产生“ warning: passing argument 1 of 'objectForKey:' discards qualifiers from pointer target type”。我渴望使我的代码不受编译器警告的影响。如何解决此警告?