views:

36

answers:

1

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?

+(NSString*)stringWithMaybeString:(id)maybeString {
    if ( [maybeString isKindOfClass:[NSString class]] )
        return maybeString;
    return nil;
}
+1  A: 

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

Possibly you could add it to NSString using a category.

willcodejavaforfood
My method is of course inside an NSString category :) But I expected there to be something built in...
Danra
@Danra - sometimes you have to do a little work yourself :)
willcodejavaforfood