I got this code and XCode warns me of "incompatible pointer types initializing NSString *__strong with an expression of type UITextField".
NSString *name = (UITextField *)searchText.text;
but this one is fine
NSString *name2 = [(UITextField *)searchText text];
and this one is fine too
NSString *name3 = [(UITextField *)searchText.text mutableCopy];
I have two questions:
- I am confused with
obj.*and[obj *] - Why is the "mutableCopy" correct is this case?
I don’t know how to search in Apple developer documentation for these questions.
In the first version, due to operator precedence, you’re casting
searchText.textto aUITextField*, what you want to do is probably to castsearchText;In the second version you don’t have the dot, so the compiler understands your cast to be casting searchText to
UITextFieldand 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;
searchText.textto aUITextField*. The runtime still knows that the object is an NSString and themutableCopymessage 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.mutableCopyreturnsid(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
searchTextoriginally? That the last version compiled without warning indicates that it’s already anUITextField*, or at least a type that can take thetextmessage. If so, you should be able to just do;