I am a bit confused when working with objective-c. This part in particular confuses me a lot.
What is the purpose and/ or difference between writing code like this …
object = [object method];
and
[object method];
Learning objective-c up until now, I always assumed that I could do something like this..
say I had already created this..
NSString *object = [[NSString alloc]initWithFormat:@"%@"];
then I could do what I want with that like so..
[object applyAnyMethodHere];
but now I’m seeing things like this ..
object = [object applyAnyMethodHere];
What is the difference between these two?
The first one (
object = [object method];) is an assignment of whatever method returns.The second one (
[object method];) is just calling the method without paying attention to its return value (if any).The third (
NSString *object = [[NSString alloc]initWithFormat:@"@"]) declares variable and assigns the return value of theinitWithFormatmethod called on the return value of theallocclass method.