将 NSRect 保存到 NSUserDefaults
目前我正在尝试将 NSRect
保存到我的用户默认值。现在,我使用 NSValue 将其包装为对象,然后使用以下代码将其作为对象保存到用户默认值中:
[userDefaults setObject:[NSValue valueWithRect:rect forKey:@"Key"]];
但是,当我运行它时,我收到一条错误消息:
[NSUserDefaults setObject:forKey:]:尝试插入“NSConcreteValue”类的非属性值“NSRect: {{0, 0}, {50, 50}}”。
有谁知道我该如何解决这个问题?
Currently I'm trying to save an NSRect
to my user defaults. Right now I'm using an NSValue
to wrap it as an object then save it into the user defaults as an object with the following code:
[userDefaults setObject:[NSValue valueWithRect:rect forKey:@"Key"]];
However, when I run it, I get an error that says:
[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value 'NSRect: {{0, 0}, {50, 50}}' of class 'NSConcreteValue'.
Does anyone know how I can fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个更简单的解决方案,使用 NSString:
Swift 4
Here is a much simpler solution, using
NSString
:Swift 4
用户默认不支持
NSValue
。您需要将值存档到数据中,然后在访问它时将其取消存档。
NSValue
is not supported by the user defaults.You need to archive the value into data then unarchive it when you access it.