-1

I am new in ios development. I want to know the difference between:

  • [object1 method];
  • object1.method;

I know it's the same thing but i still have confusion.

Thank you for help.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
Najoua Mahi
  • 304
  • 3
  • 16

4 Answers4

3

It is a bit confusing. In theory (at least according to some "authorities") the second form should only be used for properties, not regular declared methods. In practice the compiler doesn't appear to care.

But of course note that the second form can also be used as an assignment target (rather than a value source) -- object1.method = value2;. In this case the code is translated to [object1 setMethod:value2];.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • You should note that the first one is "sending a message to an object" (which hails from smalltalk) and the other is "dot notation" (which hails from C). – Oxcug Dec 01 '14 at 17:55
  • 1
    @theMonster anyway, even dot notation results in sending message. It's just a syntax sugar and convinience – marvin_yorke Dec 01 '14 at 17:57
  • @theMonster - But again in practice the iPhone developers I know refer to both styles as "calls". The message sending terminology is really only needed to decipher some of the documentation. – Hot Licks Dec 01 '14 at 17:58
  • I don't remember where I read it, but I recall reading from somewhere that even if it is not a property, we could still do `self.method` and the compiler won't complaint. As a matter of fact I just tested it, it worked (though with warning). Can someone elaborate why? – Unheilig Dec 01 '14 at 18:38
  • @Unheilig - That's what I said - "In practice the compiler doesn't appear to care." – Hot Licks Dec 01 '14 at 18:39
  • Dot syntax is allowed on whenever the left side's type can be determined at compile time. So there may be times when it won't work, especially when the left side is type id, rather than a specific type. – Jon Shier Dec 01 '14 at 18:40
2

The second form is purely "syntactic sugar" that generates the exact same code as the bracket form.

The code

object.property

invokes the method

[object property] (a getter)

and

object.property = value;

invokes the code

[object setProperty: value];

As others have pointed out, it is considered bad form to use dot syntax EXCEPT to invoke a property's getter/setter, but it does work.

Some language purists frown on the dot notation, saying that it's syntactically ambiguous

(the expression foo.bar could mean that foo is an object and bar is a property of that object,or foo could be a C struct, and bar could be a field of the struct. From that bit of code you can't tell which it is. You have to go look at the declaration of foo to tell the difference.)

I see the purist's point, but love the dot notation all the same. It's sooo much simpler to type, and to parse as well. It's easy to lose track of the bracket nesting in complex expressions using properties of properties. Dot notation makes it easier to follow the gist of the expression, and also makes it more succinct.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

Technically you can use dot notation for methods, although you shouldn't. Dot syntax is intended to use only when working with properties. For methods, use regular message syntax ([object selector];)

marvin_yorke
  • 3,469
  • 4
  • 25
  • 35
0

If you scroll down to the "Methods and Messaging" section of the following documentation link from apple, you'll see that it doesn't matter. You can send messages using square brackets or dot notation. This applies to properties or calling methods. The compiler converts the dot notation to square brackets whenever you build your app, so the code is exactly the same at runtime either way.

https://developer.apple.com/library/mac/referencelibrary/GettingStarted/RoadMapOSX/books/WriteObjective-CCode/WriteObjective-CCode/WriteObjective-CCode.html

wildBillMunson
  • 284
  • 4
  • 11