We all know that in Objective-c there are two ways to call a function:

  1. Sending a message [obj meth];
  2. Call the obj.meth; method obj.meth;

I really want to learn how to call a method with one argument in the second way. If the first way with one argument [obj meth:arg]; , what will the second method look like? It was experimentally established that the javas trick

obj.meth(arg);

in objective-c does not work.

For example, I didn’t generate simple code:

 #import "Cocoa/Cocoa.h" @interface QNObject: NSObject { int num; } @end @implementation QNObject - (int) num:(int)x //метод принимает int на вход { return 5; } @end int main(int argc, char *argv[]) { // NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - НЕ НУЖНО В ВЫСОКИХ ВЕРСИЯХ XCODE QNObject *o = [[QNObject alloc] init]; NSLog(@"%d", [o num:9]); //ПОСЫЛАНИЕ СООБЩЕНИЯ. РАБОТАЕТ НОРМАЛЬНО NSLog(@"%d", o.num(5)); // ВЫЗОВ МЕТОДА. ВЫДАЕТ ОШИБКУ // [o release]; - НЕ НУЖНО В ВЫСОКИХ ВЕРСИЯХ XCODE // [pool release]; - НЕ НУЖНО В ВЫСОКИХ ВЕРСИЯХ XCODE return 0; } 

How to rewrite it so that there is no mistake in the fifth line from the end?

  • In objective c, the methods are invoked using a message; If you want to call as a function, try to deal with the blocks. Here you can read more ru.wikipedia.org/wiki/Objective-C ; - Orest Mykha
  • With the parameter does not want to call through the point: NSLog (@ "% d", [o num: (5)]); - Orest Mykha

1 answer 1

I will try to clarify the situation.

When you declare @property , the compiler generates two methods for you - the getter and the setter. It looks like this

 @property NSString *myString; - (NSString *)myString; // сгенерированный геттер - (void)setMyString:(NSString*)aString; // сгенерированный сеттер 

then you can access this property using either square bracket notation

 NSString *myNewString = [self myString]; //getter [self setMyString:myNewString]; // setter 

either using dot notation

 NSString *myNewString = self.myString; //getter self.myString = myNewString; //setter 

That is, when I write self.myString = myNewString the compiler actually knows that he needs to transform all this and call setMyString: The side effect of this is a situation in which I can call any method using dot notification, if such a method exists (not necessarily generated for a property).

Accordingly, what you are trying to do is impossible, since there is no such mechanism in xcode, and what to convert to when the dot notification behaves both as a getter and as a setter at the same time it does not know.

UPDATE

I will try to give more examples.

If I wrote self.someMethod , xcode will look for a method named someMethod, which returns non-void. If found, then everything is fine.

If I wrote self.someMethod = (with equal at the end), xcode will look for the setSomeMethod method, which takes one parameter. Again, if found, everything will work.

This is all that xcode can do when it meets dot notification. Accordingly, your idea, where the parameter and something to return, xcode'u not known.

  • Thanks for your answer, but the following is not clear: 1. Firstly, I did not generate @property in the code. At the same time, - (int) num //метод ничего не принимает на вход { return 5; } - (int) num //метод ничего не принимает на вход { return 5; } ... - Andrew Kachalin
  • NSLog(@"%d", [o num]); //ПОСЫЛАНИЕ СООБЩЕНИЯ. РАБОТАЕТ НОРМАЛЬНО NSLog(@"%d", o.num); // ВЫЗОВ МЕТОДА работает NSLog(@"%d", [o num]); //ПОСЫЛАНИЕ СООБЩЕНИЯ. РАБОТАЕТ НОРМАЛЬНО NSLog(@"%d", o.num); // ВЫЗОВ МЕТОДА работает - Andrew Kachalin
  • That is, I want to say that rewriting a method so that it receives nothing at the input, the method works with both c dot notation and with bracket notation equally well. Why? - Andrew Kachalin
  • You have apparently missed one sentence in the answer. about the side effect - Max Mikheyenko
  • and why is the side effect when there is an argument passed to the method, and when the argument is not passed to the method, there is no side effect? - Andrew Kachalin