Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

id type to NSString

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?

like image 551
Nitesh Avatar asked Sep 27 '10 17:09

Nitesh


People also ask

What is id in objc?

id is the generic object pointer, an Objective-C type representing "any object". An instance of any Objective-C class can be stored in an id variable.

What does NSString mean?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.

What is id in Swift?

In Swift 3, the id type in Objective-C now maps to the Any type in Swift, which describes a value of any type, whether a class, enum, struct, or any other Swift type.

Do I need to release NSString?

If you create an object using a method that begins with init, new, copy, or mutableCopy, then you own that object and are responsible for releasing it (or autoreleasing it) when you're done with it. If you create an object using any other method, that object is autoreleased, and you don't need to release it.


2 Answers

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]]
like image 108
cobbal Avatar answered Sep 20 '22 07:09

cobbal


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.

like image 35
Carl Norum Avatar answered Sep 21 '22 07:09

Carl Norum