In Objective-C i saw that we can use dot operator to set and get a value and for the same task i saw something like “[ob method]” inside square braces method call, what do we call for this kind of syntax?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Bracket notation: A staple of the small talk language, and now a lovely visage of the ObjC language.
Brackets were SmallTalk’s way of saying “You there, take this message and do something with it”, and so that’s how they were implemented in Objective C. We send a message to the first part [Object] and state the message in the second part [Object Message];
Of course, they also serve a similar function with properties. Properties in most languages are written in dot notation (Object.property), but with Objective-C and the modern runtime’s support for non-ivar-backed properties, and the @synthesize directive, properties automatically generate getters named the same. Sounds complicated? It isn’t. If I have the property example, then I can access it in one of two ways:
Or
Easy!
But the @synthesize directive doesn’t stop there. We get a getter, and a setter as well. The setter can be accessed the same number of ways as a getter.
is the equivalent of
Bracket notation is in fact so important, that the compiler optimizes most dot notation out to bracket notation at compile time.