Using Properties in Objective-C Tutorial 边看边记

本文是Objective-C属性使用教程,介绍了五个关键规则:为每个实例变量创建属性,类变量使用`retain`,遵循`alloc/init/autorelease`模式,通过`self.xxx`设置变量,并在`dealloc`中释放变量。讨论了这些规则背后的内存管理及封装原则。
摘要由CSDN通过智能技术生成

Using Properties in Objective-C Tutorial

This approach definitely works, but requires you to think carefully about what’s going on with memory each time you set these variables. So let’s see if there’s an easier way!

第一次看到prtoperty是以这种形式引出来的,一般都是,嫌写getter setter方法麻烦。
When you have an instance variable such as _sushiTypes, you often want another class to be able to access it. But it’s generally bad practice to allow classes to directly access instance variables, because it makes your code brittle.
哎,我写的程序一般都是brittle的。
又得出去一趟,回来继续看。

周六早上开始看的,现在是周一晚上,好歹是看完了。文章还是不错的,以下的内容,我也从来没有再中文的文章中,看到过,很精辟

I’ll list the rules out first, and then we’ll have a discussion of each one.

  1. Always make a property for every instance variable.
  2. If it’s a class, mark it with the retain attribute. Otherwise, mark it with the assign attribute.
  3. Whenever creating a variable, use the alloc/init/autorelease idiom.
  4. Whenever setting a variable, always use “self.xxx = yyy” (in other words, use the property).
  5. For each of your instance variables, call “self.xxx = nil” in dealloc. If it’s an outlet or something you created in viewDidLoad, do the same in viewDidUnload.

Ok, now onto the discussion!

For rule #1: By making an instance variable for each property, you can let the compiler write the memory management code for you. The drawback is it doesn’t keep the private data of your class well encapsulated so can lead to more connected code if you are not careful.

For rule #2: By retaining variables whenever they are set, you can make sure that you can access your instance variables at any time.

For rule #3: When you create a variable, you want to use the alloc/init/autorelease idiom (like you can see creating the NSArray with the sushiTypes earlier). This way, the memory will be freed up for you automatically later on. If you need to keep the variable around long-term, you should assign it to a property, put it in an array, or the like to increment the reference count.

For rule #4: By using the self.xxx syntax (i.e. using the property) whenever you set a variable, you’ll go through the properties and hence make sure to release the old variable/retain the new variable. Note some programmers worry about using getters/setters/properties in initializers and dealloc, but I don’t think it’s something to worry about if you wrote & understand the code.

For rule #5: Calling “self.xxx = nil” will go through your properties and decrement the reference count. Don’t forget about viewDidUnload!

继续修炼,屌丝总有出头日
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值