2

I'm currently learning objective-c. What exactly do the square braces around things signify and is there any difference between using that and using a period (I'm from a .NET world so this would be simpler for me).

Thanks.

Cœur
  • 32,421
  • 21
  • 173
  • 232
Matt B
  • 733
  • 3
  • 9
  • 17

2 Answers2

3

They both do the same thing in your example. The . is a sort of shorthand used to access a property of an object. [] are used to send a message to the object. In your example, those happen to be the same thing. You'll notice the difference if you try to send a message that doesn't directly map to a property name.

For example: [myString length] and myString.length are the same, but if you wanted to set the length (let's assume that makes sense for the example's sake), you'd need to do something like [myString setLength:newLength] or myString.length = newLength. Besides that, there are messages that aren't the same as property names - like this example:

[myString stringByReplacingOccurrencesOfString:@"hello" withString:@"world"];

which has no meaningful equivalent using the . shorthand.

Carl Norum
  • 201,810
  • 27
  • 390
  • 454
  • Interesting, I could have sworn that the compiler _used_ to let you send messages with no arguments to objects via the dot operator, even if the message didn't' map to a property name. Not that I ever did that, of course... – Matt Wilding Mar 15 '12 at 22:31
  • That's possible - I haven't tried it. I wouldn't say that would be a good idea, though. I certainly wouldn't want to write a program that relied on that behaviour. – Carl Norum Mar 15 '12 at 22:32
  • @Carl: The compiler does indeed allow that. It doesn't distinguish between a read-only property and a parameterless method with the right return value. After all, the getter for a property is a parameterless method. After all, `myArray.count;` does in fact do a `[myArray count];`. – Rudy Velthuis Mar 15 '12 at 22:40
  • Yeah it makes sense. I just think it's strange - if said method returned `void`, for example, would you be seeing code like `myObject.method;` around? Weeeeeeird. – Carl Norum Mar 15 '12 at 22:45
1

Objective-C uses messages, rather than methods, and that's the main syntax (the dot syntax was introduced as an alternative for simple get/set messages). There are obviously significant similarities between methods and messages, but also differences.

In Objective-C, objects have complete flexibility in responding to a message at runtime. They can handle a totally unexpected message, for instance by proxying it to another object. Or, you could do things like map messages to columns at runtime. This is based on a legacy going back to Smalltalk.

.NET only just got this flexibility with dynamic/DynamicObject/IDynamicMetaObjectProvider. Of course, in either language, method/message names defined at compile-time are usually more appropriate. But there are notable use cases for dynamic ones.

Of course, Objective-C is a superset of C, so the . is used for other things (generally structs or unions).

Matthew Flaschen
  • 255,933
  • 45
  • 489
  • 528