开发者

Best way to check for variable's class in objective C?

开发者 https://www.devze.com 2023-01-19 10:21 出处:网络
I have an object which I am not sure is an NSString or not (could be NSNull, for example when reading a json into an NSDictionary) and I would like to get an NSString* if it is a valid string, nil oth

I have an object which I am not sure is an NSString or not (could be NSNull, for example when reading a json into an NSDictionary) and I would like to get an NSString* if it is a valid string, nil otherwise.

Is there an accepted way of doing this except writing my own function?

+(NSStrin开发者_Go百科g*)stringWithMaybeString:(id)maybeString {
    if ( [maybeString isKindOfClass:[NSString class]] )
        return maybeString;
    return nil;
}


Your method looks like the accepted way of doing this to me :)

Possibly you could add it to NSString using a category.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号