iPhone Obj-C属性

Dev*_*ted 1 iphone properties objective-c

我是一本关于创建iPhone应用程序的书的初学者.

其中一步是写"UISwitch*whichSwitch = whichSwitch.isOn;" 而我只是好奇"isOn"来自哪里?

在文档中:

on
A Boolean value that determines the off/on state of the switch.

@property(nonatomic, getter=isOn) BOOL on
Run Code Online (Sandbox Code Playgroud)

那个"getter = isOn"部分意味着什么?我提出这个问题的最终原因是因为我想知道当我遇到不同UI元素的类似情况时应该做些什么.

哦,是的,这就像属性创建"setSomething"mutator和"something"访问器的东西?除了布尔值,它是"isOn"和"on"?

谢谢.

Tim*_*Tim 6

属性基本上是以后生成方法的简写(实际创建是通过@synthesize实现文件中的指令完成的).该getter=isOn内部@property确实意味着getter方法具有名称isOn.

默认情况下,属性将创建一个与ivar同名的getter 和一个带有setpreped 的setter .更改getter名称(或其setter,使用setter=语法)就是这个属性指令所做的.你应该只对布尔或类似的变量这样做 - 其他变量应该有一个与变量同名的getter.