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.
精彩评论