Objective-c Id,sel,bool,nil,null and Nsnull of data types

Source: Internet
Author: User

Id

The ID is a pointer to the Objective-c object, equivalent to void* in C, which can map any object pointer to him, or map it to other objects. The common ID type is the delegate property of the class.

SEL

The SEL type is a keyword that OBJECTIVE-C uses to define a method, but never belongs to any class instance, whose value is computed by @selector and can be used as a function pointer, but the actual type is const char*. The SEL class can be created directly at compile time via @selector (), or it can be created with the nsselectorfromstring () function, which allows the method to be called by name, with the following code:

    1. [Object performselector: @selector (dosometing)];

Equivalent to:

    1. [Object dosometing];

The Objective-c object can accept any message, and when it is not necessarily able to respond to the message, the Respondstoselector () function can often be used in encoding to determine whether an object can correspond to a message, thereby protecting the code:

    1. NSString *childselectorname = [NSString stringwithformat:@ "add%@", Akey];
    2. SEL chilidselector = nsselectorformstring (childselectorname);
    3. if ([self repondstoselector:chilidselector]) {
    4. [Self performselector:chilidselector withobject:achild];
    5. }

If no protection is made, and the object does not respond with a corresponding method, the program runs out of a run-time exception.

BOOL

The Boolean type in OBJECTIVE-C is bool, and its value can be either yes or no, or it can be assigned true and Flase. Yes and true are equivalent, all are non-0 values, no and false equivalent, are 0 values. When you call, you can print the value by printing a number, the code is as follows:

    1. NSString *childselectorname = [NSString stringwithformat:@ "add%@", Akey];
    2. SEL chilidselector = nsselectorformstring (childselectorname);
    3. if ([self repondstoselector:chilidselector]) {
    4. [Self performselector:chilidselector withobject:achild];
    5. }

Nil , NULL and the NSNull

Note: Controlling improper handling is one of the important causes of program errors and even crashes.

Because Objective-c collection objects, such as Nsarray,nsdictionary,nsset, can contain Nsnull objects, it can cause a program crash if item in the first line of code is nunull:

    1. NSString *item = [Nsarray objectatindex:i];
    2. if ([Item isequaltostring:@ "Testnumber"]) {
    3. //
    4. }

The following code is a common error, and the release object is not set to nil, causing the program to crash.

    1. ID someobject = [[Object alloc] init];
    2. //...
    3. [Someobject release];
    4. //...
    5. if (someobject) {
    6. Crash here
    7. }

Nil is used to assign values to an object (any object of objective-c is of type ID), NULL assigns any pointer, null and nil are not interchangeable, nil is used for class pointer assignment (an object of class in Objective-c, an instance of class Meta-class) , while Nsnull is used to combine operations, although they represent control, but the use of the situation is completely different, so in the code is strictly according to the variable type to assign values, the correct null value to the correct type, the code is easy to read and maintain, it is not easy to cause errors.

Objective-c has a feature that when sending a message to a nil object, the system returns a value of 0 instead of causing an exception because nil is the legal value of the object and the Nil object can also accept the message.

Nil

Nil is the literal null value of the OBJC object, the object of the ID type, or the OBJC object declared with the @interface.

Nil

Nil is a written null value for the OBJC class type, corresponding to the class type object.

Null

Null is an arbitrary C-pointer null value.

NSNull

NSNull is a class that represents a null value and is a OBJC object. In fact, it has only one singleton method: +[nsnull null], which is typically used to represent objects whose values are empty in the collection.

1.nil

pointer to an object is empty in the objc.h the definitions in are as follows :

in the objective-c used in ID type of Object

NSString *name = nil;

Nsurl *url = nil;

ID object = nil;

2.Nil

a pointer to a class is empty defined as follows:

in the objective-c used in Class type of Object

Class aclass = Nil;

Clsss Bclass = [Nsurl class];

3.NULL

Pointing C the pointer to type is empty in the stddef.h is defined in the following :

more for the following example :

int *pint = NULL;

char *chchar = NULL;

struct ststruct = NULL;

4.NSNull

in the objective-c is a class in , There's just one in the name. Null , NSNull have a + (NSNull *) null; single-instance method, more for collections (nsarray,nsdictionary) An object with an empty medium value

Nsarray *array = [Nsarray arraywithobjects:

[[NSObject alloc] init],

[NSNull NULL],

@ "AAA",

Nil

[[NSObject alloc] init],

[[NSObject alloc] init], nil];

NSLog (@ "%ld", Array.count); Output 3,nsarray ends with nil

Nsdictionary *dictionary = [[Nsdictionary alloc] Initwithobjectsandkeys:

@ "Object0", @ "Key0",

@ "Object1", @ "Key1",

Nil, @ "Key-nil"

@ "Object2", @ "Key2",

NIL];

NSLog (@ "%@", dictionary); Output of 2 Key-value,nsdictionary is also terminated with nil

Nsmutabledictionary *mutabledictionary = [[Nsmutabledictionary alloc] init];

[Mutabledictionary setobject:nil forkey:@ "Key-nil"]; Will cause crash is encountered unknown problem, the program ran out of error. Crash is the meaning of the crash [mutabledictionary setobject:[nsnull null] forkey:@ "Key-nil"]; does not cause crash

So in use, the following methods are relatively safe

[Mutabledictionary setobject: (nil = = value?) [NSNull NULL]: value) forkey:@ "Key"];

Objective-c Id,sel,bool,nil,null and Nsnull of data types

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.