The study of Nsnull

Source: Internet
Author: User

Why do you have nsnull?

Objective-c is a superset of C, which mainly introduces the design concept of OO. Therefore, objective-c inevitably uses pointers and pointer variables to describe the memory address of an object. So, since there is a pointer to this thing, of course, the existence of a null pointer, that is, a null pointer.

In addition, objective-c mainly defines two types of containers: Nsarray and nsdictionary, and specifies that no nil pointers can be placed in either of these containers, only the NSObject object is stored. Then there are some scenarios that do need to save nil pointers, possibly for the purpose of guaranteeing the correctness of count. As a result, the Nsnull object is stored in the position where the nil pointer is needed to represent an empty object, except that it has no practical meaning. That's why cocoa needs to design nsnull.

The problems brought by Nsnull

Although the Nsnull can guarantee the correctness of the container count, it is a hassle for our business. While Nsarray and nsdictionary can hold different types of objects, they are usually stored in the same type of object, so that we can iterate through the container directly using foreach . However, Nsnull has brought us problems, we are not sure whether the container contains Nsnull objects, each container needs to filter the inside of the Nsnull objects contained, if not processed to assume that the container does not contain nsnull, it is likely to bring crash.

The null type defined in Sqlite3 and the JSON data from the server will generate a corresponding Nsnull object. Even the use of KVO causes Nsnull objects to spread throughout the system.

A technique for replacing nsnull

If you do a filter nsnull every time you use a container, you may get a few questions:

    • Additional computational capacity.
    • Redundant code.
    • The code is slightly odd.

Recall that two types of data are most commonly used: numbers and strings, corresponding to NSNumber and NSString in Objective-c. And objective-c as a dynamic binding language, which is what we often call Runtime binding. Each method in Objective-c contains a implementation and selector,Runtime The corresponding implementation is found through the Objective-c class object model based on the selector received by each object. The runtime of OBJECTIVE-C provides a forwarding technology that can forward messages that cannot be processed by the object to an object that can respond to the message, and if it is not possible to find an object that responds to the message, Then a nsinvalidargumentexception will be thrown. With this mechanism, we can return a default object that can respond to a message based on the received message. For example, receiving a charvalue message can return a @0 object, and receiving a intvalue message returns an @ "" object. Code is as follows:

- (id)forwardingTargetForSelector:(SEL)aSelector {    if ([NSString instancesRespondToSelector:aSelector]) {      return @"";    }else if ([NSNumber instancesRespondToSelector:aSelector]) {       return @0;         }else {    return nil;     }}

Based on this technique, the Gngeneralnullvalue class is designed to automatically replace the system-defined nsnull, and all messages destined for the Nsnull class are sent to the objects produced by the Gngeneralnullvalue class. It then returns an object with a default value that can be responded to, based on the received message. At the same time, Gngeneralnullvalue supports adding user-defined classes, in addition to the basic data types. Based on the unit test code you write, you can get a glimpse of how it is used.

- (void)test_NSNumber_charValue_shouldResponse {    NSNumber *number = (NSNumber *)[GNGeneralNullValue     generalNullValue];       XCTAssertEqual(0, [number charValue]);}- (void)test_NSNumber_floatValue_shouldResponse {    NSNumber *number = (NSNumber *)[GNGeneralNullValue     generalNullValue];    XCTAssertEqual(0.0f, [number floatValue]);}

Its specific implementation and source address for Gngeneralnullvalue, welcome feedback problems and communication. It is very convenient and convenient to use, just contain the source code, no other operation can enjoy the life away from Nsnull.

The study of Nsnull

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.