需要在iOS中解释@ property / @ synthesize [英] Need explanation on @property/@synthesize in iOS

查看:102
本文介绍了需要在iOS中解释@ property / @ synthesize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是一个完整的菜鸟我对iOS编程和Ojbective-C都很陌生。我主要来自C(DSP,微控制器),Delphi XE2 / Pascal,Visual Basic和Java(桌面和Android应用程序)的背景。

Not a complete noob I am quite new to iOS programming and to Ojbective-C. I mainly come from a background of C (DSP, Microcontrollers), Delphi XE2/Pascal , Visual Basic and Java (desktop and Android apps).

我主要学习Cocoa与来自Apress的开始iOS 5开发一书。

I mainly learned Cocoa with the book "Beginning iOS 5 Development", from Apress.

最近我观看了WWDC 2012的视频并浏览了他们的一些示例代码,我必须说我我很困惑写我的应用程序的正确方法是什么,更具体地说是@ property / @ synthesize单词。

Recently I watched videos of the WWDC 2012 and went through some of their sample code, and I must say that I am confused of what is the proper way of writing my apps and more specifically with the @property/@synthesize words.

在书中大多数(不是说全部)示例代码用于定义属性,例如

In the book most (not to say all) of the sample code uses to define a property as for example

ViewController.h
@property (strong, nonatomic) NSMutableArray *list;

ViewController.m
@synthesize list;

然后所有代码访问合成列表

then all the code access synthesize list with

self.list

甚至只是

list

现在在我阅读的每个WWDC代码示例中,我看到程序员定义了一个属性,但是在.m文件中他们执行的操作类似于

Now in every WWDC code sample I read I see that programmers define a property but then, in the .m file they do things like

@synthesize list = _list;

并且有时访问

self.list

_list

我很困惑。什么是正确的做法?由于Apple程序员都使用下划线,我认为我应该这样做,但为什么这本书没有呢? list和_list之间有区别吗?更重要的是,因为我在同一个对象中为什么有时会使用self.list,有时候会使用list / _list。

I am confused. What is the correct practice ? As Apple programmers all use underscore I think I should do it that way but why the book did not ? Is there a difference between list and _list ? And more over, as I am in the same object why sometime use self.list and sometimes list/_list.

有时它们不使用@synthesize,我认为它是当他们想重新编写自己的访问者和变种者时(到目前为止,我的情况从来都不是这样)。

Sometimes they don't use @synthesize, I assume it's when they want to re-write their own accessors and mutators (which is never my case up to now).

我在网上已经阅读过,但没有什么是足够清楚,让我的思绪正确,所以我依靠StackOverflow在这里带来光明。

I have read here and there on the web but nothing was clear enough to set my mind right, so I count on StackOverflow to bring light here.

最后但并非最不重要的我更喜欢并根据当前的iOS 6最佳实践/编程回答技术。无用告诉我如何在旧的iOS中正确地做到这一点。

Last but not least I prefer and answer based on current iOS 6 best practice/programming technique. Useless to tell me how to do it correctly in older iOS.

推荐答案

没有正确的方法。只有你喜欢的风格。

There is no correct way. There is only your preferred style.

最新的编译器对属性声明进行隐式综合。

The lastest compilers do an implicit synthesise on the property declaration.

@synthesize list = _list;

您的代码中没有写入任何内容。它只是发生。

Nothing is ever written in your code. It just happens.

然而,这并没有阻止你明确地这样做。

However that doesnt stop you doing this explicitly.

@合成list = somethingelse;

所以当你要求指向 list 的指针时访问者( self.list )你会得到一个指向 somethingelse的指针

So when you ask for a pointer to list via the accessor (self.list) you will get a pointer to somethingelse

在大多数情况下 NSMutableArray * thing = self.list 相当于 NSMutableArray * thing = somethingelse

In most cases NSMutableArray *thing = self.list is equivalent to NSMutableArray *thing = somethingelse

仅仅因为Apple使用了一种风格并不意味着你必须这样做。每家公司通常都有自己的编码风格。

And just because Apple uses a style doesn't mean that you have to do it. Each company usually has their own coding style.

使用 @synthesize list; 的主要问题是它构成您可以写的风险

The main problem with using @synthesize list; is that it poses the risk that you can write either

self.list = thing list = thing

前者使用sythesised setList:访问器,后者则没有,并且存在相关错误的风险你的代码虽然不如 ARC 那么糟糕,因为你不会发生 strong 属性的泄密。

The former uses the sythesised setList: accessor while the latter doesn't and put the risk of related bugs in your code , though its not as bad with ARC as you dont get leaks happening for strong properties.

你使用什么风格,保持一致,并注意直接使用ivar list = thing 的效果使用其访问者 self.list = thing

What ever style you use, keep it consistent and be aware of the effects of using an ivar directly list = thing as compared to using its accessor self.list = thing

这篇关于需要在iOS中解释@ property / @ synthesize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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