微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NSValue和NSNumber的作用和关系

 

NSNumber

NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned charshort intintlong intlong long intfloat,or double or as a BOOL. (Note that number objects do not necessarily preserve the type they are created with.) It also definescompare: method to determine the ordering of two NSNumber objects.

Creating a Subclass of NSNumber

As with any class cluster,if you create a subclass of NSNumber,you have to override the primitive methods of its superclass, NSValue. Furthermore,there is a restricted set of return values that your implementation of theNSValue method objCType can return,in order to take advantage of the abstract implementations of the non-primitive methods. The valid return values are “c”,“C”,“s”,“S”,“i”,“I”,“l”,“L”,“q”,“Q”,“f”,and “d”.

 

NSValue

An NSValue object is a simple container for a single C or Objective-C data item. It can hold any of the scalar types such as int,float,and char,as well as pointers,structures,and object ids. The purpose of this class is to allow items of such data types to be added to collections such as instances of NSArray and NSSet,which require their elements to be objects. NSValue objects are always immutable.

 

NSData 

NSData and its mutable subclass NSMutableData provide data objects,object-oriented wrappers for byte buffers. Data objects let simple allocated buffers (that is,data with no embedded pointers) take on the behavior of Foundation objects.

NSData creates static data objects,and NSMutableData creates dynamic data objects. NSData and NSMutableDataare typically used for data storage and are also useful in distributed Objects applications,where data contained in data objects can be copied or moved between applications.

Using 32-bit Cocoa,the size of the data is subject to a theoretical 2GB limit (in practice,because memory will be used by other objects this limit will be smaller); using 64-bit Cocoa,the size of the data is subject to a theoretical limit of about 8EB (in practice,the limit should not be a factor).

NSData is “toll-free bridged” with its Core Foundation counterpart, CFData Reference. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore,in a method where you see an NSData * parameter,you can pass a CFDataRef,and in a function where you see aCFDataRef parameter,you can pass an NSData instance (you cast one type to the other to suppress compiler warnings). This also applies to your concrete subclasses of NSData. See Interchangeable Data Types for more @R_730_4045@ion on toll-free bridging.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐