Problems with IOS development (nil refers to the impact of Nsdictionary and Nsarray initialization)

Source: Internet
Author: User

Nil refers to the effects of nsdictionary and Nsarray initializationrecently, when I was doing a project, I encountered a pit crash, which was caused by a nil pointer when Nsdictionary was initialized. Suppose we are now initializing a nsdictionary of {key1:value1, key2:value2, key3:value3}, there are generally two ways to initialize it:

1, the use of standard initialization method:

NSDictionary *dictionary =[[NSDictionaryalloc] initWithObjectsAndKeys:value1,@"key1",value2,@"key2", value3 ,@"value3",nil];

2. New supported initialization methods after using ios6.0:

NSDictionary *dictionary [email protected]{@"key1" : value1,@"key2" : value2,@"key3" : value3};

Now we assign a value to the value1 value2 Value3 and set the value2 to the nil pointer:

NSString *value1 =@"value1";NSString *value2 =nil;NSString *value3 =@"value3";

If you use the second initialization method, the running program will find a crash with the following log:

DICTIONARYTEXTDEMO[29390:1329578] terminating app due to uncaught exception ' nsinvalidargumentexception ', Reason: ' -[__nsplaceholderdictionary InitWithObjects:forKeys:count:]: Attempt to insert nil object from objects[1] '

The discovery system will prevent you from inserting a nil pointer to object[1] and query the official Apple document to get

Discussion
This method steps through the objects Andkeys arrays, creating entries in the new dictionary as it goes. Annsinvalidargumentexception is raised if a key or value object Isnil.

This means that the initialization method must be used to ensure that both key and value are not nil, so we need to judge the initialization before initializing it, and if Nil does not join the dictionary. However, if there is a need for value to be empty, value can be assigned [NSNull null] so that it can be successfully inserted into the dictionary, and the final printout is

DICTIONARYTEXTDEMO[29510:1338517] Dictionary: {key1 = Value1;key2 = ""; value3 = Value3;}

Another workaround is to use the standard initialization method, which is the first initialization method. But at the same time also pay attention to a problem, using

NDictionary *dictionary = [[NSDictionaryalloc]initWithObjectsAndKeys:value1,@"key1",value2,@"key2", value3 ,@"value3",nil];

If we set one of the keys or value to the nil pointer, then the system will be judged to complete the entire object insertion, which is equivalent to the last nil we initialized the array and the dictionary. This approach, though, avoids the exception thrown when the nil pointer is inserted, but can be a major security risk in a project and is difficult to spot. Therefore, it is recommended to use the second initialization method, and to make the necessary judgment on the nil pointer, and use it selectively [NSNull null] to realize the demand of the feature. Similarly, the use of Nsarray should also pay attention to such problems.

Think: Want to use {Key1:value1,key2 "", key3:value3} but to judge that no value can be nil; and use,initWithObjectsAndKeys:value1;不会崩溃,但是 系统会认为是末尾的nil,造成参数不完整。调试时难以发现。

1. Note the initialization of variables.

2. When assigning an operation, the object after the = sign is judged.

其实想写个类别是想实现,对nil 校验,nil 自动转换为@“”;还没想好!

Problems with IOS development (nil refers to the impact of Nsdictionary and Nsarray initialization)

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.