I am new to Objective-C programming. In many tutorials I have been seeing code with this format:
[[classname function] function];
For example:
[[NSString alloc] initwithformat:parameters];
When I was learning about plist and dictionaries I saw this line of code:
[[self.objname objectAtIndex:indexPath.row] objectForKey:@"somename"];
I believe it follows the syntax: [[objectname function] function]
Can someone please explain how this syntax works and the difference between the first and second example.
Also it would be really helpful if you can provide the equivalent statements in C or Java for these examples.
[ClassName methodName]is how you call a class method. The equivalent in Java isClassname.methodName();[object methodName]is how you call a normal instance method. The equivalent in Java isobject.methodName();In Objective C you can also ‘nest’ calls in
[]brackets.[[Classname method1] method2]is equivalent to calling the class methodmethod1, which returns some object and then callingmethod2on that object.The equivalent in Java would be
Classname.method1().method2();Also note that in strict Objective C terminology you don’t ‘call a method on an object’, you ‘send a message to an object’ instead. Same thing, different words.