ios 学习笔记

1. Whenever you create an object in viewDidLoad (setting the reference count to 1), you should release the object in viewDidUnload.

 

2. It is a good practice to set an object to nil after release, because by setting it to nil it avoids a lot of problems. Any time you call a method on a nil object, it does nothing, but if you don’t set it to nil, if you tried calling a method on a deallocated object your program should crash.

 

3. Property attributes:

nonatomic, which tells the compiler not to worry about multithreading.

retain, which tells the compiler to retain the passed-in variable before setting the instance variable.

assign, which tells the compiler NOT to retain the passed-in variable.

copy, which makes a copy of the passed-in variable before setting.

 

4. Following list the rules for memory management, 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.

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. 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!

 

5. 关于Build Settings里的Build Active Architecture Only选项:

这个属性设置为yes,是为了debug的时候编译速度更快,它表示只编译当前的architecture版本。

而设置为no时,会编译所有的版本。

这个是设备对应的architecture:

armv6:iPhone 2G/3G,iPod 1G/2G
armv7:iPhone 3GS/4/4s,iPod 3G/4G,iPad 1G/2G/3G
armv7s:iPhone5, iPod5

编译出的版本是向下兼容的,比如你设置此值为yes,用iphone4编译出来的是armv7版本的,iphone5也可以运行,但是armv6的设备就不能运行。

所以,一般debug的时候可以选择设置为yes,release的时候要改为no,以适应不同设备。

posted @ 2013-05-24 10:35  九勺吹雪  阅读(154)  评论(0)    收藏  举报