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?
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.
A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.
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.
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.
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]]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With