简体   繁体   English

obj c函数中(+)和(-)运算符有什么区别

[英]What is the difference between (+) and (-) operator in obj c function

Hi i am new to iphone and starts to learning Obj c. 嗨,我是iphone新手,开始学习Obj c。

i have notice the function definition, for some function we are using (-) and for some function we are using (+) 我注意到函数定义,对于某些功能,我们正在使用(-),对于某些功能,我们正在使用(+)

Example: + (id)requestWithURL:(NSURL *)theURL 示例:+(id)requestWithURL:(NSURL *)theURL

– initWithURL: – initWithURL:

what the difference between those two operator / symbol's usage? 这两个运算符/符号的用法有什么区别?

thanks! 谢谢!

In the context of a class definition, +/- determine whether the methods are instance or class level methods. 在类定义的上下文中, +/-确定方法是实例方法还是类级别方法。

+ indicates the method is class level, and you don't need an instance to call it. +表示方法是类级别的,您不需要实例来调用它。

- indicates the method is an instance method, and must be called through an instance of an object. -表示该方法是实例方法,必须通过对象的实例调用。

A common example of a static (+) method is NSString::stringWithFormat , when you call it, you do so without an instance, but rather using the class name: 静态(+)方法的常见示例是NSString::stringWithFormat ,当您调用它时,它没有实例,而是使用类名:

[NSString stringWithFormat: @"Your age is %d", age];

An instance method must be called on an instance of an appropriate object, an example of one would be: 必须在适当对象的实例上调用实例方法,其中一个示例是:

NSString *s = @"oop:ack:zonks::ponies";
int len = [s length]; // instance method called

These symbols should not be confused with the mathematical operators + and - , which can only be applied as part of binary or unary arithmetic expressions. 这些符号不应与数学运算符+-混淆,后者只能用作二进制或一元算术表达式的一部分。

+ = Static methods (ie you don't need an instance of the class to call the method - but you can't use non-static member variables or anything like that) + =静态方法(即,您不需要类的实例来调用该方法-但您不能使用非静态成员变量或类似的方法)

- = Instance methods - =实例方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM