The difference between classes and objects in iOS and Nil,nil,null

Source: Internet
Author: User

I. Classes and objects

Concepts of classes and objects:

1. A class is a highly abstract class that defines the static properties (attributes) and dynamic properties (methods) that this class of objects should have.

2. An object is an instance of a class and is a concrete thing

3. A class is actually a data type whose variable is the object

4. Relationship between classes and classes--inheritance relationship

Eg: students and pupils, pupils are also students, so students are the parents of pupils, pupils are the children of the class

OC vs. Object-oriented:

1. The object is the core of the OC program. "Everything is Object" is the core idea in the procedure

2. A class is a template used to create objects of the same type, defining the member variables and methods that the class object has in a class

3. Classes can be considered as a collection of static attributes (instance variables) and dynamic properties (methods)

The 4.IOS SDK provides a large number of classes for programmers to use, and programmers can define their own classes

Two. The difference between nil,nil,null

1.

Nil: null pointer to an object in OC

Nil: null pointer to class in OC

NULL: Pointers to other types of NULL pointers, such as a C-type memory pointer

NSNull: in the Collection object, represents the empty worthy object

If obj is nil:

[obj message] will return no, not nsexception

If obj is nsnull:

[obj message] throws an exception nsexception

2.

Nil and the NULL It is simpler to understand the literal meaning that nil is an object, and null is a value, and I understand that nil is set to NULL, and NUL is set to NULL, and we do not generate crash or throw exceptions for the nil call method.

eg

Nsurl *url = nil;

Class class = Nil;

int *pointerint = NULL;

Nil is an object pointer is empty, nil is a class pointer is empty, NULL is the basic data type is empty.

These five concepts are easy to confuse, and in some cases they have the same usage, first explaining the meaning of some null pointers, which means that the pointer does not point to a meaningful memory area. such as: int *p,int *p = NULL;

NSObject *obj1 = [[NSObject alloc] init];

NSObject *obj2 = [NSNull null];

NSObject *obj3 = [NSObject new];

NSObject *obj4;

Nsarray *array = [Nsarray arraywithobjects:obj1,obj2,obj3,obj4];

NSLog (@ "Array count:%ld", [array Count]); Count:3, because obj = nil, adds an object at the end of the join OBJ4

NSObject *obj1;

NSObject *obj2 = [[NSObject alloc] init];

NSObject *obj3 = [NSNull null];

NSObject *OBJ4 = [NSObject new];

Nsarray *array = [Nsarray arraywithobjects:obj1,obj2,obj3,obj4];

NSLog (@ "Array count:%ld", [array Count]); Count:0, because Obj1 is nil, there is no way to add the object behind it.

[NSNull null] usually acts as a placeholder, as follows:

NSObject *obj1 = [NSNull null];

Nsarray *arr1 = [Nsarray arraywithobject:@ "one", @ "one", obj1,@ "three", nil];

For (NSString *str in arr1) {

NSLog (@ "Array object:%@", str);

}//result:one,two,three

The difference between classes and objects in iOS and Nil,nil,null

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.