How to check the NULL value in NSString in iOS?

前端 未结 10 1596
盖世英雄少女心
盖世英雄少女心 2021-01-02 15:17

I have an NSString and I want to check if it has a NULL value. If it does, then the if condition should execute. Else it should execut

10条回答
  •  死守一世寂寞
    2021-01-02 15:46

    In Objective-C and Cocoa, the property may not be set—that is, it's nil—or it may be set to the object representation of nil, which is an instance of NSNull. You probably want to check for either of these conditions, like this:

    NSString* categoryName = appDelegate.categoryName;
    if (categoryName == nil || categoryName == (id)[NSNull null]) {
      // nil branch
    } else {
      // category name is set
    }
    

    This will execute the nil branch if the categoryName property is set to nil (the default for all properties), or if it's been explicitly set to the NSNull singleton.

提交回复
热议问题