Objective-C是@synthesize必需还是可选的? [英] Objective-C is @synthesize required or optional?

查看:182
本文介绍了Objective-C是@synthesize必需还是可选的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,我是一名新手iOS开发人员,最近我简要地听说@synthesize现在是某个版本的Xcode中的@synthesize,并且编译器会自动合成和属性并将_分配给私有变量。



我已经测试了这个:



没有@synthesize,你可以使用

访问属性和私有字段

  self.property_name = something; 
_property_name =某事; //(用于getter和setter)

使用@synthesize property_name,可以访问属性和私有字段使用

  self.property_name = something; 
property_name = something;

我的问题是我们真的需要@synthesize吗?还是有一个我错过的更大的故事?

解决方案

从Xcode 4.4开始,我们不需要这样做,它添加了一个名为默认合成属性的功能。



简单地说,它会自动生成:

  @synthesize name = _name; 


sorry I'm a newbie iOS developer, recently I've briefly heard that @synthesize is now @synthesize in a certain version of Xcode and that the compiler will auto synthesize and properties and assign _ to private variables.

I've tested this:

with out @synthesize, you can access properties and private field using

self.property_name = something;
_property_name = something; //(used in getter and setters)

with @synthesize property_name, you can access properties and private field using

self.property_name = something;
property_name = something;

My question is do we really need @synthesize anymore? or there is a bigger story I'm missing?

解决方案

No we don't need to do that as of Xcode 4.4, which added a feature called Default Synthesis Of Properties.

Simply put, it generates this automatically:

@synthesize name = _name;

这篇关于Objective-C是@synthesize必需还是可选的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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