How to extract NSString from user defined object?

I have an object, suppose class A, and it contains a string in it. I want to print it in NSString. Basically this is what I want to achieve:

NSString *temp = (NSString *)[A someMEthod];

if(temp isMemberOfClass : [NSString class]) {
    dosomething
}

Can someone tell me how to do this?

+3
source share
3 answers

If you want to check an object if it belongs to a specific class (at runtime), use this:

if ([myObj isKindOfClass:[NSString class]]) { ... }

Your question is not entirely clear, it seems that your best option is to make the "string stored in the object" another property of the NSString type, so you do not have to do any clicks and type checks at all.

0
source
id temp = [A someMethod];
if([temp isKindOfClass:[NSString class]])
{
    NSString *str = (NSString *)temp;
    NSLog(@"%@", str);
}
+3
source

If your string is a member variable of ClassA.

You can create a method in ClassA that returns an NSString.

.

@interface ClassA :NSObject{

NSString *str;

}

-(NSString *)returnString{

return str;

}
+1
source

All Articles