Objective-c中+和-方法的区别

时间:2021-05-19 21:28:19

What is the difference between methods that are declared with - and methods that are declared with +

用-声明的方法和用+声明的方法之间有什么区别

e.g

- (void)methodname

+ (void)methodname

5 个解决方案

#1


42  

Methods prefixed with - are instance methods. This means they can only be invoked on an instance of a class, eg:

以-为前缀的方法是实例方法。这意味着它们只能在类的实例上调用,例如:

[myStringInstance length];

Methods prefixed with + are class methods. This means they can be called on Classes, without needing an instance, eg:

以+开头的方法是类方法。这意味着可以在类上调用它们,而不需要实例,例如:

[NSString stringWithString:@"Hello World"];

#2


2  

minus are instance methods (only accessible via an instantiated object)

负的是实例方法(仅可通过实例化对象访问)

plus are class methods (like in Java Math.abs(), you can use it without an instantited object)

另外还有类方法(如在Java Math.abs()中,您可以使用它而无需实例化对象)

#3


1  

According to this page:

根据这个页面:

Instance methods begin with - and class level methods begin with +

实例方法以-和类级别方法以+开始。

See this SO question for more information.

更多信息请参见这个问题。

#4


0  

The first is an instance method and the second is a class method. You should read Apple's Objective-C documentation to learn about the difference.

第一个是实例方法,第二个是类方法。您应该阅读苹果的Objective-C文档以了解其中的差异。

#5


0  

+(void)methodname is a class variable and -(void)methodname is object variable.

+(void)methodname是类变量,-(void)methodname是对象变量。

Lets say you make a utility class that has a method to reverse string. The class you call MYUtility.

假设您创建了一个实用程序类,该类具有反转字符串的方法。称为MYUtility的类。

If you use +, like

如果你用+,比如

+ (NSString *)reverse:(NSString *)stringToReverse

You could use it directly like

你可以直接使用

NSString *reversed = [MYUtility stringToReverse:@"I Love objective C"];

if you used a -, like

如果你使用-,比如。

- (NSString *)reverse:(NSString *)stringToReverse

You have to use :

你必须使用:

MYUtility *myUtil = [[MYUtility alloc] init];
NSString *reversed = [myUtil stringToReverse:@"There are many ways to do the same thing"];

With the class based function, you just call directly, but you don't have access to any local variables besides #defines that you can do because the class isn't instantiated.

使用基于类的函数,您只需直接调用,但是除了#定义之外,您不能访问任何本地变量,因为类没有实例化。

But with the - (NSString you must instantiate the class before use, and you have access to all local variables.

但是对于- (NSString,在使用之前必须实例化类,并且可以访问所有的本地变量。

This isn't a pick one thing and stay with it, many classes have both, just look at the header file for NSString, it is littered with + functions and - functions.

这并不是一件简单的事情,很多类都有,看看NSString的头文件,它到处都是+函数和-函数。

#1


42  

Methods prefixed with - are instance methods. This means they can only be invoked on an instance of a class, eg:

以-为前缀的方法是实例方法。这意味着它们只能在类的实例上调用,例如:

[myStringInstance length];

Methods prefixed with + are class methods. This means they can be called on Classes, without needing an instance, eg:

以+开头的方法是类方法。这意味着可以在类上调用它们,而不需要实例,例如:

[NSString stringWithString:@"Hello World"];

#2


2  

minus are instance methods (only accessible via an instantiated object)

负的是实例方法(仅可通过实例化对象访问)

plus are class methods (like in Java Math.abs(), you can use it without an instantited object)

另外还有类方法(如在Java Math.abs()中,您可以使用它而无需实例化对象)

#3


1  

According to this page:

根据这个页面:

Instance methods begin with - and class level methods begin with +

实例方法以-和类级别方法以+开始。

See this SO question for more information.

更多信息请参见这个问题。

#4


0  

The first is an instance method and the second is a class method. You should read Apple's Objective-C documentation to learn about the difference.

第一个是实例方法,第二个是类方法。您应该阅读苹果的Objective-C文档以了解其中的差异。

#5


0  

+(void)methodname is a class variable and -(void)methodname is object variable.

+(void)methodname是类变量,-(void)methodname是对象变量。

Lets say you make a utility class that has a method to reverse string. The class you call MYUtility.

假设您创建了一个实用程序类,该类具有反转字符串的方法。称为MYUtility的类。

If you use +, like

如果你用+,比如

+ (NSString *)reverse:(NSString *)stringToReverse

You could use it directly like

你可以直接使用

NSString *reversed = [MYUtility stringToReverse:@"I Love objective C"];

if you used a -, like

如果你使用-,比如。

- (NSString *)reverse:(NSString *)stringToReverse

You have to use :

你必须使用:

MYUtility *myUtil = [[MYUtility alloc] init];
NSString *reversed = [myUtil stringToReverse:@"There are many ways to do the same thing"];

With the class based function, you just call directly, but you don't have access to any local variables besides #defines that you can do because the class isn't instantiated.

使用基于类的函数,您只需直接调用,但是除了#定义之外,您不能访问任何本地变量,因为类没有实例化。

But with the - (NSString you must instantiate the class before use, and you have access to all local variables.

但是对于- (NSString,在使用之前必须实例化类,并且可以访问所有的本地变量。

This isn't a pick one thing and stay with it, many classes have both, just look at the header file for NSString, it is littered with + functions and - functions.

这并不是一件简单的事情,很多类都有,看看NSString的头文件,它到处都是+函数和-函数。