Objective-C澄清; -/+和* var [英] Objective-C Clarification; -/+ and *var

查看:71
本文介绍了Objective-C澄清; -/+和* var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一本书(<用于Mac OS X的 Cocoa编程)中自学Objective-C,但是大约一半了,但是我有两个未在本书中回答或定义的问题.

I'm teaching myself Objective-C from a book (Cocoa programming for mac OS X) and am about halfway through however I have two questions that aren't answered or defined in the book.

  1. 在定义类方法时(假设在.h文件中)之间有什么区别:

  1. When defining class methods what is the difference between (assuming there in a .h file):

-(国际)人口;

+(国际)人口;

我目前的看法是-方法要求首先分配和初始化该类,但是+可以静态调用而无需分配和初始化.例如. (在另一个类的函数中)

The way I see it at the moment is that - methods require the class to be allocated and initialized first however + can be called statically without requiring allocation and initialization. E.g. (in a function in another class)

// Using -
Earth *world = [[Earth alloc] init];
int population = [world population];

// Using +
int population = [Earth population];

如果这是正确的,我什么时候应该使用静态方法,这样做对他们不利吗?

If that is correct, when should I use static methods and are they're any disadvantages with doing so.

  1. 在函数参数中定义var或在函数中将其定义为实际var时,使用*表示var将成为对象吗?例如(同样在头文件中.)

  1. When defining a var in either a function paramater or as an actual var in a function, does the use of * mean the var will be an object? e.g. (again in a header file.)

-(void)setPopulation:(NSNumber *)人口; //使用*作为人口是NSNumber

- (void) setPopulation: (NSNumber *) population; //Use of * as population is of NSNumber

-(无效)setPopulation:(int)人口; //人口不是课程,因此不需要*

- (void) setPopulation: (int) population; // population isn't a class so doesn't need *

很抱歉,如果我的任何一个术语在诸如静态方法之类的Objective-C领域都没有意义.我是PHP和Ruby程序员.

Sorry if any of my terms don't make sense in the land of Objective-C such as static methods, etc. I'm a PHP and Ruby Programmer.

推荐答案

Objective-C方法声明中的-/+仅表示该方法是类方法还是实例方法.例如,使用Objective-C,您无法向实例发送被声明为类方法的消息.例如:

The -/+ in method declarations for Objective-C simply denote whether the method is a class method or an instance method. For example, with Objective-C, you cannot send an instance a message that was declared as a class method. For example:

@interface MyObject : NSObject
-(void)myInstanceMethod;
+(void)myClassMethod;
@end

// ...

MyObject* obj = [[MyObject alloc] init];

[obj myInstanceMethod];      // this is okay
[obj myClassMethod];         // this will fail
[[obj class] myClassMethod]; // this is okay
[MyObject myClassMethod];    // this is okay
[MyObject myInstanceMethod]; // this will fail

对于问题的第二部分,Objective-C是C的严格超集.它添加了类,但它们实际上是C数据结构,其实现被Objective-C运行时隐藏了.因此,类始终表示为指针.在C语言中,*表示变量已声明为指向某个内存地址的指针.您也可以在C语言中使用具有原始类型的指针,但是Objective-C对象必须始终由指针引用.

As to the second part of your question, Objective-C is a strict super-set of C. It adds classes but they are really C data structures whose implementations are hidden from you by the Objective-C runtime. Because of this, classes are always represented as pointers. In C, the * means that the variable is being declared as a pointer to some memory address. You can use pointers with primitive types in C as well, but Objective-C objects must always be referred to by pointers.

这里有许多很棒的指向指针的教程/介绍.我建议只使用Google搜索C教程和指针以了解更多信息.

There are many great tutorials/introductions to pointers out there. I would suggest simply googling for C tutorial and pointers to learn more.

这篇关于Objective-C澄清; -/+和* var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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