IOS ----- class and object, nil/Nil/NULL, ios ----- nil

Source: Internet
Author: User

IOS ----- class and object, nil/Nil/NULL, ios ----- nil
Differences between classes and objects in iOS, nil/Nil/NULL
Concepts of classes and objects

A class is an abstraction of the height of a class of things. It defines the static attributes (attributes) and dynamic attributes (methods) that an object should have ).

An object is an instance of a class and a specific thing.

Class and object are abstract and specific relationships.

A class is actually a data type, and its variables are objects.

Relationship between classes-inheritance relationship

A is B

If this sentence is used, it can be regarded as an inheritance relationship when designing a program.

For example, students and pupils are also students, so students are the parent class of pupils and pupils are child classes of students.

Oc and object-oriented

Objects are the core of the oc program. "Everything is an object" is the core idea of the program.

A class is a template used to create objects of the same type. A class defines the member variables and methods of this class object.

A class can be seen as a collection of static attributes (instance variables) and dynamic attributes (methods.

The iOS SDK provides a large number of classes for programmers. programmers can also define their own classes.

 

Differences between nil, Nil, and NULL

Nil: a null pointer to an object in oc.

Nil: a null pointer to the class in oc.

NULL: point to other types of NULL pointers, such as a c-type memory pointer. The basic data type is NULL and the basic type.

NSNull: an object that represents a null value in a collection object.

If obj is nil:

[Obj message] NO instead of NSException will be returned

If obj is NSNull:

[Obj message] will throw an NSException


Nil and NULL are simple to understand. nil is an object and NULL is a value. I understand that nil is used to set the object to NULL, NULL sets the basic type to NULL. In addition, the nil call method does not generate crash or throw an exception.
Take a look at the following code:
// Nil points to the object
NSURL * url = nil;
// Nil points to Class class Class = Nil;
// NULL is the basic data type set to NULL int * pointerInt = NULL;
Nil is an object pointer with NULL values, Nil is a class pointer with NULL values, and NULL is a basic data type with NULL values.

These five concepts are easily obfuscated and have the same usage in some cases. First, explain the meaning of "Null Pointer". a null pointer indicates that the pointer does not point to a meaningful memory area. For example, int * p; int * p = NULL;
NSObject * obj1 = [[NSObject alloc] init]; NSObject * obj2 = [NSNull null]; NSObject * obj3 = [NSObject new]; NSObject * obj4; NSArray * arr1 = [NSArray arrayWithObjects: obj1, obj2, obj3, obj4, nil]; NSLog (@ "arr1 count: % ld", [arr1 count]); // count: 3 because obj = nil, NSObject * obj1; NSObject * obj2 = [[NSObject alloc] init]; NSObject * obj3 = [NSNull null] ends when obj4 is added; NSObject * obj4 = [NSObject new]; NSArray * arr2 = [NSArray arrayWithObjects: obj1, obj2, obj3, obj4, nil]; NSLog (@ "arr2 count: % ld ", [arr2 count]); // count: 0. Because obj1 = nil, the following object is not added to [NSNull null]

// Usually acts as a placeholder, as follows: NSObject * obj1 = [NSNull null]; NSArray * arr1 = [NSArray arrayWithObjects: @ "One", @ "TWO", obj1, @ "three", nil]; for (NSString * str in arr1)
{NSLog (@ "array object: % @", str) ;}// result: One, Two, three NSObject * obj1 = [NSNull null]; NSArray * arr1 = [NSArray arrayWithObjects: @ "One", @ "TWO", obj1, @ "three", nil]; for (NSString * str in arr1)
{If (str! = [NSNull null])
{NSLog (@ "array object: % @", str) ;}// result: One, Two, three

 

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.