Using a constant NSString as the key for NSUserDefaults

前端 未结 4 1057
借酒劲吻你
借酒劲吻你 2021-01-29 18:27

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 c

4条回答
  •  有刺的猬
    2021-01-29 19:07

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

    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 * const prevents you from reassigning kPoly to point to a new NSString object.

提交回复
热议问题