What do the plus and minus signs mean in Objective

2019-01-02 23:01发布

I am very new in objective c and in xcode. I would like to know what the + and - signs next to a method definition mean.

- (void)loadPluginsAtPath:(NSString*)pluginPath errors:(NSArray **)errors;

4条回答
萌系小妹纸
2楼-- · 2019-01-02 23:16

The definitive explanation of this from Apple is here, under the 'Methods and Messaging' section:

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

In brief:

+ means 'class method'

(method can be called without an instance of the class being instantiated). So you call it like this:

[className classMethod]; 


- means 'instance method'

You need to instantiate an object first, then you can call the method on the object). You can manually instantiate an object like this:

SomeClass* myInstance = [[SomeClass alloc] init];

(this essentially allocates memory space for the object then initalises the object in that space - an oversimplification but a good way to think about it. You can alloc and init the object seperately but never do this - it can lead to nasty issues related to pointers and memory management)

Then call the instance method:

[myInstance instanceMethod]

An alternative way to get an instance of an object in Objective C is like this:

NSNumber *myNumber = [NSNumber numberWithInt:123];

which is calling the 'numberWithInt' class method of the NSNumber class, which is a 'factory' method (i.e. a method that provides you with a 'ready made instance' of an object).

Objective C also allows the creation of certain object instances directly using special syntax, like in the case of a string like this:

NSString *myStringInstance = @"abc";

查看更多
冷血范
3楼-- · 2019-01-02 23:25

+ methods are class methods - that is, methods which do not have access to an instances properties. Used for methods like alloc or helper methods for the class that do not require access to instance variables

- methods are instance methods - relate to a single instance of an object. Usually used for most methods on a class.

See the Language Specification for more detail.

查看更多
地球回转人心会变
4楼-- · 2019-01-02 23:28

(+) for class methods and (-) for instance method,

(+)class methods:-

Are methods which are declared as static. The method can be called without creating an instance of the class. Class methods can only operate on class members and not on instance members as class methods are unaware of instance members. Instance methods of the class can also not be called from within a class method unless they are being called on an instance of that class.

(-)Instance methods:-

On the other hand require an instance of the class to exist before they can be called, so an instance of a class needs to be created by using the new keyword. Instance methods operate on specific instances of classes. Instance methods are not declared as static.

查看更多
戒情不戒烟
5楼-- · 2019-01-02 23:31

+ is for a class method and - is for an instance method.

E.g.

// Not actually Apple's code.
@interface NSArray : NSObject {
}
+ (NSArray *)array;
- (id)objectAtIndex:(NSUInteger)index;
@end

// somewhere else:

id myArray = [NSArray array];         // see how the message is sent to NSArray?
id obj = [myArray objectAtIndex:4];   // here the message is sent to myArray

// Btw, in production code one uses "NSArray *myArray" instead of only "id".

There's another question dealing with the difference between class and instance methods.

查看更多
登录 后发表回答