Syntax for calling a method in Objective-C?

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

 [object foo]; 
+6
source share
4 answers

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

 anInstance.foo = @"bar"; 

which literally translates at compile time

 [anInstance setFoo:@"bar"]; 

(similar to "getters")

Other methods are more advanced, for example, using NSObject performSelector: method or other systems such as NSInvocation, etc. Going deeper, there are ways to call methods at runtime with c functions (all this syntax ultimately boils down to calling c functions); but I'm sure that is not what you need.

+1
source

You can use KVC:

  [label setValue:@"Some text" forKey:@"text"]; 
+4
source

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

+1
source

For pedantry, why not get low:

 objc_msgSend(object, sel_getUid("foo"), errVar); 
+1
source

Source: https://habr.com/ru/post/918267/


All Articles