views:

48

answers:

5

is there any way by which I can change an id type to NSString object? note the following line of my code.

NSString *value = [appDelegate.bird_arr objectAtIndex:rowClicked] ;

in this appDelegate is object of my AppDelegate class in a navigation based program and bird_arr is object of NSMutableArray. I want to use the string written in row which is clicked. But objectAtIndex: returns id type. Do we have any way to change this id type to NSString or any other way by which I can collect string inside the specific row?

+1  A: 

yep, just cast it

NSString *value = (NSString *)[appDelegate.bird_arr objectAtIndex:rowClicked];

If you want to double check that it really is an NSString use

[someId isKindOfClass:[NSString class]]
cobbal
No need to cast -- an `id` is a generic object, and so can be assigned to a pointer of any type.
mipadi
+2  A: 

An id could be a pointer any object, and it will get promoted to any object pointer type automatically (sort of like void * in C). The code you show there looks correct. There's no need for an explicit cast, though you could add one if you really want to.

If the object you're getting out of the array is not, in fact, an NSString, you will have to do some other work to get the object you want.

Carl Norum
A: 

assuming that the object is an NSString, casting to NSString* should work fine:

NSString* value = (NSString*)[appDelegate.bird_arr objectAtIndex:rowClicked];
jstevenco
+1  A: 

"id type" just tells us the return value is an object — nothing beyond that. It could already be an NSString object, or it could be something else. If all the objects in this array are strings, your work is done. If they're something else, you need some way to get that something else into a string.

Chuck
A: 

casting can be done to be on safer side but, is not needed here. I tried few things withe code after both casting and without casting and things worked fine. Id holds the reference and what type of reference is something done at runtime, i suppose. However I found another better way to get text at a specific row in string format. I done that using cellForRowAtIndex:index method. this will return me the cell at specified index, then using textLabel property. Thanx for your reply and help Guys.

Nitesh