5

I come from the Java world, so to me it's all object.foo(), but in Objective C, is object messaging the only way to invoke a method?

[object foo];
Cezar
  • 55,636
  • 19
  • 86
  • 87
raffian
  • 31,267
  • 26
  • 103
  • 174

4 Answers4

4

You can use KVC:

 [label setValue:@"Some text" forKey:@"text"];
Igor
  • 4,235
  • 3
  • 34
  • 32
1

Yes. You can use dot syntax to get or set Objective C properties. So for example setting text on a UILabel *label can be done either [label setText:@"some text"]; or label.text = @"some text";

Ladislav
  • 7,223
  • 5
  • 27
  • 31
  • What about `->`, as in label->setText("some text"); ? – raffian Jun 17 '12 at 04:20
  • See here regarding -> http://stackoverflow.com/questions/4486048/what-is-in-objective-c – Chris Trahey Jun 17 '12 at 04:22
  • Dot notation is much simple, for properties, at least, to hell with the brackets ;-) – raffian Jun 17 '12 at 04:29
  • 1
    @ctrahey's link is good. As explained the foo->bar accesses the bar member of the C struct pointed to by foo. There is no Objective-C involved, nor is this a way to call a method. Object instance variables can be accessed this way because objects in Objective-C are fundamentally just structs, with their ivars being members of the struct. – Andrew Madsen Jun 17 '12 at 05:09
1

The first thing that comes to mind is using @property and dot-notation. A class with @property named 'foo' allows you to do like this:

anInstance.foo = @"bar";

which literally translates at compile-time to

[anInstance setFoo:@"bar"];

(similar with "getters")

The other ways are more advanced, such as using NSObject's performSelector: method, or other systems such as NSInvocation, etc. Going deeper, there are ways to call methods in the runtime, with c functions (all of this syntax eventually boils down to calling c-functions anyway); but I'm sure that's not what you are after.

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • Are there performance considerations when using `[]` over `.`? – raffian Jun 17 '12 at 04:24
  • Actually, you used the phrase, "is object messaging the only way to invoke a method?", so the technical answer is "no". Messaging translates to the runtime function calls, and you can use them, too. In fact, you've touched on one of the core "features" of the runtime, dynamic binding of a message to a method. The "method" (or, implementation to be more precise) is looked up at runtime for every message sent. Check this out: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/Introduction.html – Chris Trahey Jun 17 '12 at 04:25
  • 1
    They are actually identical when the program actually runs. I mean literally. – Chris Trahey Jun 17 '12 at 04:26
  • @RaffiM Here is a great answer to your question about performance considerations: – pasawaya Jun 17 '12 at 04:28
  • What I'm after are all the syntactical ways to invoke a method. – raffian Jun 17 '12 at 04:28
  • Yes, that concern (in the accepted answer there) hints that dot notation is converted to message send when the app is compiled, and it can be 'tricked' into calling any method. It doesn't actually touch your instance variables (in and of itself). – Chris Trahey Jun 17 '12 at 04:31
1

For pedantry's sake, why not get low-level:

objc_msgSend(object, sel_getUid("foo"), errVar);
CodaFi
  • 43,043
  • 8
  • 107
  • 153