开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜