在 Objective-C 中,方法旁边的加号和减号是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2097294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What do the plus and minus signs mean in Objective-C next to a method?
提问by gyurisc
In Objective-C, I would like to know what the +and -signs next to a method definition mean.
在 Objective-C 中,我想知道方法定义旁边的+和-符号是什么意思。
- (void)loadPluginsAtPath:(NSString*)pluginPath errors:(NSArray **)errors;
回答by Georg Sch?lly
+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.
回答by Vandit Mehta
(+) 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.
另一方面,需要类的实例存在才能调用它们,因此需要使用 new 关键字创建类的实例。实例方法对类的特定实例进行操作。实例方法未声明为静态方法。
How to create?
如何创造?
@interface CustomClass : NSObject
+ (void)classMethod;
- (void)instanceMethod;
@end
How to use?
如何使用?
[CustomClass classMethod];
CustomClass *classObject = [[CustomClass alloc] init];
[classObject instanceMethod];
回答by Robert Christie
+ 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
+ 方法是类方法 - 即,无法访问实例属性的方法。用于不需要访问实例变量的类的 alloc 或 helper 方法等方法
- methods are instance methods - relate to a single instance of an object. Usually used for most methods on a class.
- 方法是实例方法 - 与对象的单个实例相关。通常用于类中的大多数方法。
See the Language Specificationfor more detail.
有关更多详细信息,请参阅语言规范。
回答by Chris Halcrow
The definitive explanation of this from Apple is here, under the 'Methods and Messaging' section:
Apple 对此的最终解释在这里,在“方法和消息”部分下:
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:
在 Objective C 中获取对象实例的另一种方法是这样的:
NSNumber *myNumber = [NSNumber numberWithInt:123];
which is calling the 'numberWithInt' class methodof the NSNumber class, which is a 'factory' method (i.e. a method that provides you with a 'ready made instance' of an object).
它正在调用NSNumber 类的 'numberWithInt'类方法,这是一个“工厂”方法(即为您提供对象的“现成实例”的方法)。
Objective C also allows the creation of certain object instances directly using special syntax, like in the case of a string like this:
Objective C 还允许使用特殊语法直接创建某些对象实例,就像这样的字符串:
NSString *myStringInstance = @"abc";
NSString *myStringInstance = @"abc";

