NSNumber与Int [英] NSNumber vs Int

查看:150
本文介绍了NSNumber与Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果整数不能写入字典然后写入.plist,但NSNumbers可以更好地在整个应用程序中使用NSNumbers,而不是需要转换每次保存或从.plist中加载字典吗? / p>

If integers cannot be written to a dictionary and then to a .plist, but NSNumbers can is it better to use NSNumbers throughout the app, rather than needing to convert every-time saving or loading a dictionary from a .plist?

推荐答案

作为概括:坚持使用POD类型,直到你需要来使用基于对象的表示,例如 NSNumber 。 POD的性能要好得多,但在某些情况下你需要 NSNumber

As a generalization: Just stick with POD types until you need to use an object based representation, such as NSNumber. The performance is much better with the PODs, but you'll need NSNumber in some cases.

在某些情况下,使用 NSNumber 可能是有意义的 - 这通常是当你重用 NSNumber 经常 - 这是为了避免大量重复 NSNumber s。除了序列化和通用objc接口(绑定,变换器,字典)之外,这种情况很少发生。

In some cases, it may make sense to use NSNumber instead -- this is typically when you reuse a NSNumber often -- this is to avoid making a ton of duplicate NSNumbers. Such occurrences are practical only rarely beyond serialization and generic objc interfaces (bindings, transformers, dictionaries).

更新/详细信息: ObjC运行时在某些情况下,在某些体系结构上,在某些操作系统版本上替换标记指针代表 NSNumber 特定类型和域名的。虽然自几年前最初写的内部代表发生了变化,但这里有一个很好的介绍: http://objectivistc.tumblr.com/post/7872364181/tagged-pointers-and-fast-pathed-cfnumber-integers-in 。在可以使用它的地方,它可以避免分配,锁定和引用计数操作等慢速操作。尽管如此,标记指针无法表示每个数字并且会引入开销,因此您仍然应该优先使用 NSNumber 作为默认值的基本内置函数。标记指针在适用的情况下是一个很好的优化,但是当你只需要一个数字时,它远没有与内置竞争。

Update/Details: The ObjC runtime will in some cases, on some architectures, and on some OS versions substitute a tagged pointer representing NSNumbers of specific type and domain. Although the internal representation has changed since originally written a few years back, here is a good introduction to the subject: http://objectivistc.tumblr.com/post/7872364181/tagged-pointers-and-fast-pathed-cfnumber-integers-in. Where this can be used, it saves you from slow operations like allocations, locking, and ref count ops. Nevertheless, tagged pointers are incapable of representing every number and it introduces overhead, so you should still favor basic builtins over NSNumber as a default. Tagged pointers are a great optimization where applicable, but are far from competing with the builtins when you just need a number.

这篇关于NSNumber与Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆