“+”之间的区别和“ - ”在Objective-C中的函数名之前 [英] Difference between "+" and "-" before function name in Objective-C

查看:94
本文介绍了“+”之间的区别和“ - ”在Objective-C中的函数名之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C程序的函数名接口声明之前,+和 - 之间有什么区别。例子:

   - (void)continueSpeaking; 

+(NSArray *)availableVoices;

有什么区别?

解决方法 定义了一个类方法

类方法属于本身,而不是实例



示例: [AppDelegate someMethod]
$ - 定义一个实例方法



示例 [[[UIApplication sharedApplication] delegate] someMethod]



描述差异的一种方法是 - 方法对对象操作,而 + 方法对类本身进行操作。



假定你的类被命名为 MyClass ,并且你创建了它的一个实例并将它存储到一个名为 myInstance
$ b - (void)continueSpeaking 可以这样调用: [myInstance但是,方法 +(NSArray *)avai

lableVoices 只能这样调用: [MyClass availableVoices]


What is the difference between "+" and "-" before the function name interface declaration in an Objective-C program. Example:

- (void)continueSpeaking;

+ (NSArray *)availableVoices;

What's the difference?

解决方案

+ defines a class method

Class methods belong to the class itself, not instances of the class.

Example: [AppDelegate someMethod]

- defines an instance method

Example [[[UIApplication sharedApplication] delegate] someMethod]

One way to describe the difference is that - methods operate on objects, while + methods operate on the class itself.

Say your class was named MyClass, and you created an instance of it and stored it into a variable called myInstance:

- (void)continueSpeaking can be called like so: [myInstance continueSpeaking].

However, the method + (NSArray *)availableVoices can only be called like so: [MyClass availableVoices]

这篇关于“+”之间的区别和“ - ”在Objective-C中的函数名之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆