Objective-C learn and learn-10: various values (nsnumber, nsvalue, nsnull) in the Foundation kit quick tutorial

Source: Internet
Author: User

5. Various numerical values
Nsarray and nsdictionary can only store objects, but cannot directly store any basic types of data, such as int, float, or struct. However, you can use an object to encapsulate basic values. For example, encapsulate int type data into an object, and then you can put this object into nsarray or nsdictionary.
1) nsnumber
Cocoa provides the nsnumber class for packaging (that is, implemented in the form of objects) Basic data types.
For example:

 + (NSNumber *) numberWithChar: (char) value;
+ (NSNumber *) numberWithInt: (int) value;
+ (NSNumber *) numberWithFloat: (float) value;
+ (NSNumber *) numberWithBool: (BOOL) value;

 

 

After encapsulating the basic data type into nsnumber, you can obtain it again using the following instance method:

 - (char) charValue;
- (int) intValue;
- (float) floatValue;
- (BOOL) boolValue;
- (NSString *) stringValue;

 

Term: Packaging a basic type of data into an object is called boxing. Extracting basic types of data from an object is called unboxing ). Objective-C does not support automatic packing.

2) nsvalue
Nsnumber is actually a subclass of nsvalue. nsvalue can wrap any value. You can use the following class method to create a new nsvalue:

 + (NSValue *) valueWithBytes: (const void *) value
objCType: (const char *) type;

 

The passed parameter is the address of the value you want to wrap (such as an nssize or your own struct ), the address of the variable you want to store is usually obtained (use the operator in C Language &). you can also provide a string (the objctype parameter) to describe the data type, which is usually used to describe the type and size of the struct entity. You do not need to write this string by yourself. The @ encode compiler command can receive data type names and generate appropriate strings. Therefore, add nsrect to nsarray as follows:

 // Put nsrect into nsarray
Nsrect rect = nsmakerect (1, 2,100,200 );
Nsvalue * rectvalue = [nsvalue valuewithbytes: & rect
Objctype: @ encode (nsrect)];
[Array addobject: rectvalue];

// Use getvalue to extract the value
// The address of the variable whose parameter is to be stored
Rectvalue = [array objectatindex: 0];
[Rectvalue getvalue: & rect];

 

In the above getvalue: In the example, get in the method name indicates that we provide a pointer, and the space pointed to by the pointer is used to store the data generated by the method.

Cocoa provides a convenient way to convert common struct data to nsvalue:

 + (NSValue *) valueWithPoint: (NSPoint) point;
+ (NSValue *) valueWithSIze: (NSSize) size;
+ (NSValue *) valueWithRect: (NSRect) rect;

 

 - (NSPoint) pointValue;
- (NSSize) sizeValue;
- (NSRect) rectValue;

 

For example:

 value = [NSValue valueWithRect: rect];
[array addObject: value];
// ….
NSRect anotherRect = [value rectValue];

 

3) nsnull
Because nil in nsarray and nsdictionary has a special meaning (indicating the end of the list), the NL value cannot be placed in the set. To store a value indicating "nothing", you can use the nsnull class. There is only one nsnull method:

 + (NSNull *) null;

For example:

 [contact setObject: [NSNull null] forKey: @"home fax"];

The access is as follows:

 id homefax;
homefax = [contact objectForKey: @"home fax"];
if (homefax == [NSNull null]
{
//...
}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.