سؤال

What is meaning of the expression:

-(someClass *)someName; 

How would you read/describe it in plain English?

Why do we put * inside brackets, and not before someName?

Do we allocate an object by this * or that just syntax?

Is it a case of "Casting"?

Is there a difference between Casting and returning a pointer?

هل كانت مفيدة؟

المحلول

The - identifies the method as an instance method, as opposed to the +, which identifies the method as a class method.

The return data type is the part inside the parenthesis. You can't return an object by value, so you must return a pointer to an object, hence the * inside the parenthesis. You are returning a pointer to someClass.

The beginning of the method name follows the parenthesis.

You would call this method someName, and it returns a pointer to an object of someClass, and it is an instance method.

If you were returning a primitive data type, the * would be unneeded because you can return primitive data types. However, you could still return a pointer to a primitive data type, in which case you'd want the *.

For example:

-(int)someName; //returns an integer
-(int *)someOtherName; //returns a pointer to an integer

نصائح أخرى

That's the Objective-C semantic for the return value of a method.

This:

-(someClass *)someName;

Means that calling someName will return a pointer to a someClass object.

For example:

someClass *myClass = self.someName;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top