How to check the NULL value in NSString in iOS?

前端 未结 10 1591
盖世英雄少女心
盖世英雄少女心 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:44

    The NULL value for Objective-C objects (type id) is nil.

    While NULL is used for C pointers (type void *).

    (In the end both end up holding the same value (0x0). They differ in type however.)

    In Objective-C:

    nil (all lower-case) is a null pointer to an Objective-C object.
    Nil (capitalized) is a null pointer to an Objective-C class.
    NULL (all caps) is a null pointer to anything else (C pointers, that is).
    [NSNull null] (singleton) for situations where use of nil is not possible (adding/receiving nil to/from NSArrays e.g.)
    

    So to check against NSNull one can either use:

    if ((NSNull *)myString == [NSNull null])
    

    or if one wants to omit the need of casting to NSNull:

    if ([myString isKindOfClass:[NSNull class]])
    

提交回复
热议问题