为什么我们要为@property声明变量 [英] Why should we declare variables for @property

查看:50
本文介绍了为什么我们要为@property声明变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective的新手.当我阅读许多教程时,@ property具有类型的变量,@ inferface中也声明了相同的变量.这需要吗?

I am newbie in Objective. As I read many tutorial, @property has a variable for a type, the same variable is declared in @inferface too. Is this needed?

示例

@interface MyInterface : NSObject
{
   NSInteger myVaribale;
}
@property(retain) NSInteger myVariable; 

在这两个地方都声明了myVariable.

Here myVariable is declared in both place.

推荐答案

自iOS 4起,您可以编写

since iOS 4, you can write

@interface MyInterface : NSObject {
}

@property(assign) NSInteger myVariable; 

@implementation MyInterface
@synthesize myVariable;
@end

这意味着您可以在接口声明中省略NSInteger myVaribale,只要在.m中进行合成即可(合成将创建setter,getter和instance变量)

Meaning you can omit the NSInteger myVaribale in your interface declaration as long as you synthesize it in the .m (the synthesize will create setter, getter and instance variable)

缺点是您不会在Xcode的调试器中看到myVariable的值.

A drawback is that you won't see the value of myVariable in Xcode's debugger.

这篇关于为什么我们要为@property声明变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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