IOS 8:nsnull is not valid in nsuerdefaults empty processing

Source: Internet
Author: User

Keyword: nsuserdefaults NSNull Nil

Recently, in some code you see the following notation:

1Nsuserdefaults *user =[Nsuserdefaults standarduserdefaults];2     3NSString *count = [user objectforkey:@"Count"];4NSString *pass = [user objectforkey:@"Pass"];5 6 if((nsnull*) Count = = [NSNullNULL])//empty, when Count is null, it will be in if, anti-collapse, because if it is empty, also need to use, it may collapse7 {8    return;9}

The result of the code comment was not obtained by experiment. The first time you run this code, count and pass are nil. (NSNull *)countafter conversion, or nil, and [NSNull null] do = = operation, the result is false, can not achieve defensive programming purposes. The debug code and the results are as follows:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions {     *hello = [[Nsuserdefaults standarduserdefaults] Valueforkey:@ "Hello"];     if NULL]) // empty, when Count is null, it will be in if, anti-collapse, because if it is empty, also need to use, it may collapse     {        return  YES;    }         return YES;}

IOS 8:nsnull is not valid in nsuerdefaults empty processing

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.