Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我没有为 NSUserDefaults 创建一个可变副本,而是使用 setObject: forKey: 进行更新,指向在我的 UI 的不同位置派生的构造对象。这样做有什么陷阱吗?特别是,我想确保我没有添加新对象,而是实际上替换了现有对象。
TIA
按照自己的方式做事应该没有任何问题。
我不确定你为什么会有这种疑问,但只要你使用相同的密钥setObject:forKey,当然会更新相同的对象而不是添加新的对象。为了避免添加新对象,我通常使用静态NSString作为键。
setObject:forKey
NSString