In the first version, due to operator precedence, you're casting searchText.text
to a UITextField*
, what you want to do is probably to cast searchText
;
NSString *name = ((UITextField *)searchText).text;
In the second version you don't have the dot, so the compiler understands your cast to be casting searchText to UITextField
and send the text message to it. In other words, exactly right.
The last case is a bit tricky since it involves both runtime and compile time. As I understand it;
- You cast
searchText.text
to a UITextField*
. The runtime still knows that the object is an NSString and the mutableCopy
message that exists on both will go to the correct method [NSString mutableCopy] anyway and create/return a mutable copy of the NSString so runtime it works out ok.
- Since
mutableCopy
returns id
(referencing a NSMutableString), the assignment to an NSString is ok by the compiler (id can be assigned to anything), so compile time it's ok.
A question, what is searchText
originally? That the last version compiled without warning indicates that it's already an UITextField*
, or at least a type that can take the text
message. If so, you should be able to just do;
NSString *name3 = [searchText.text mutableCopy];