objective-c 使用常量 NSString 作为 NSUserDefaults 的键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/753755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:24:26  来源:igfitidea点击:

Using a constant NSString as the key for NSUserDefaults

objective-ccocoadefault-value

提问by Olly

I'm using NSUSerDefaults to store user preferences. I remember reading somewhere that setting the keys as constants is a good idea - and I agree. The following code is what I currently have:

我使用 NSUSerDefaults 来存储用户首选项。我记得在某处读到将键设置为常量是个好主意 - 我同意。以下代码是我目前拥有的:

[[NSUserDefaults standardUserDefaults]
        setObject:[NSNumber numberWithInt:polygon.numberOfSides] 
           forKey:@"polygonNumberOfSides"];

I tried changing this to:

我尝试将其更改为:

@implementation Controller

NSString const *kPolygonNumberOfSides = @"polygonNumberOfSides";

-(void)savePolygonInfo {
    [[NSUserDefaults standardUserDefaults]
            setObject:[NSNumber numberWithInt:polygon.numberOfSides] 
               forKey:kPolygonNumberOfSides];
}

While this does work, it produces "warning: passing argument 1 of 'objectForKey:' discards qualifiers from pointer target type". I'm keen to keep my code free from compiler warnings. How can I fix this warning?

虽然这确实有效,但它会产生“ warning: passing argument 1 of 'objectForKey:' discards qualifiers from pointer target type”。我很想让我的代码不受编译器警告的影响。我该如何解决这个警告?

回答by e.James

You should use:

你应该使用:

NSString * const kPolygonNumberOfSides = @"..."; // const pointer

instead of:

代替:

NSString const * kPolygonNumberOfSides = @"..."; // pointer to const

The first is a constant pointer to an NSString object, while the second is a pointer to a constant NSString object.

第一个是指向 NSString 对象的常量指针,而第二个是指向常量 NSString 对象的指针。

It is a subtle difference. The compiler warning happens because setObject:forKey:is declared as follows:

这是一个微妙的区别。发生编译器警告setObject:forKey:是因为声明如下:

- (void)setObject:(id)value forKey:(NSString *)defaultName;

It is expecting the defaultNameargument to be of type NSString *. When you instead pass in a pointer to a constant, you've given it something different.

它期望defaultName参数的类型为NSString *。当你传入一个指向常量的指针时,你给了它一些不同的东西。

Update:I want to point out that these constants should be defined as staticif they are only going to be used from within a single file. I say this because I have run across this problem myself: if you do not declare them as static, then they will exist in the global namespace, and you will not be able to use a variable with the same the same name in another file. see Constants in Objective-Cfor more information. To explain by example, this is what I currently use for keys that I only need to use in one .mfile:

更新:我想指出这些常量应该被定义为static它们只在单个文件中使用。我这样说是因为我自己也遇到过这个问题:如果你不将它们声明为静态,那么它们将存在于全局命名空间中,并且你将无法在另一个文件中使用同名的变量。有关更多信息,请参阅Objective-C 中的常量。举例说明,这就是我目前用于只需要在一个.m文件中使用的密钥的方法:

static NSString * const kSomeLabel = @"...";

回答by Adam Rosenfield

Don't use constwith Objective-C objects, they weren't really designed to use it. NSStringobjects (among many others) are already immutable by default by virtue of their design, so making them constis useless.

不要const与 Objective-C 对象一起使用,它们并不是真正设计来使用它的。 NSString对象(以及许多其他对象)由于其设计而在默认情况下已经是不可变的,因此创建它们const是无用的。

As e.James suggested, you can use an NSString * const, which is a constant pointer to an NSString. This is subtly different from a const NSString *(equivalent to NSString const *), which is a pointer to a constant NSString. Using a NSString * constprevents you from reassigning kPolyto point to a new NSStringobject.

正如e.James 所建议的,您可以使用 an NSString * const,它是一个指向 an 的常量指针NSString。这与 a const NSString *(等价于NSString const *)略有不同,后者是指向常量 的指针NSString。使用 aNSString * const可以防止您重新分配kPoly指向新NSString对象。

回答by malhal

For access from other classes:

从其他类访问:

.h

。H

extern NSString * const PolygonNumberOfSidesPrefsKey;

.m

.m

NSString * const PolygonNumberOfSidesPrefsKey = @"PolygonNumberOfSides"

For access only inside current class:

仅在当前类中访问:

.m

.m

static NSString * const kPolygonNumberOfSidesPrefsKey = @"PolygonNumberOfSides"

回答by Abizern

I would suggest even making the constant more descriptive. A constant for the number of sides of a polygon could come from anywhere. As a suggestion, how about:

我建议甚至让常量更具描述性。多边形边数的常数可以来自任何地方。作为建议,如何:

kDefaultsPolygonNumberOfSides;

instead.

反而。

回答by Sean Patrick Murphy

For additional background about this issue, there is an excellent article on Wikipedia explaining the constant syntax with pointers: http://en.wikipedia.org/wiki/Const_correctness#Pointers_and_references

有关此问题的其他背景信息,维基百科上有一篇出色的文章用指针解释了常量语法:http: //en.wikipedia.org/wiki/Const_correctness#Pointers_and_references