《iOS5 programming cookbook》学习笔记1

(以前写在本地的,现在发上来)

1.16看的不是很明白,可能是因为之前没有吧。

When our app starts for the first time, we willinitialize the strong string1 property and will assign string1to string2. We will then set the value of the string1 property to nil. Then we will wait. This isabsolutely crucial. If immediately after setting the value of string1 to nil, you print out the value of string2,chances are that you will get incorrect resultsinstead of nil. So you needto make sure that your app's runloop has gotten rid of all invalidated objects.In order to achieve this, we will print the value of strong2when our app gets sent to the background. (This iscaused by the user bringing another app to the foreground.) Once we're runningin the background, we know that the runloop has already gotten rid of invalidatedobjects in the memory and the results that we will get will be accurate:

这段话里面提到了一个runloop的概念,我可以深究一下。这个我之前不知道,应该不在arc的模式下,也是有这个概念的。

1.17刚开始就看不懂这个单词Typecasting

问了一下身边的英语大神,也是不得其解。看着看着,

Typecasting is the process ofpointing one value of type A to another value of type B.原来文中自己有解释。

Remember that Automatic Reference Counting does not workfor Core Foundation objects, so we need to assist the compiler.

__bridge

Simply typecasts the object on the right side of theequation to the left side. This will not modify the retain count on any of theobjects; neither the one on the left nor the one on the right side of theequation.

__bridge_transfer

This typecast will assign the object on the right side tothe object on the left and will release the object on the right side. So if youhave a Core Foundation string, like the one we saw before, that you have justcreated and want to place it inside a local variable of type NSString (local variables are bydefault strong, seeRec- ipe 1.16), then youshould use this typecasting option because then you wouldn't have to releasethe Core Foundation string after the assignment. We will see an exmaple of thissoon.

__bridge_retained
This issimilar to the __bridge_transfer typecast, but will retain the object on the right side ofthe equation as well.

看到这里了,下火车了,未完待续。

当日,深夜10.56丈母娘家继续,

这123在了解了上面的规则之后,都能看明白,

1. We allocated a Core Foundation string and placed itinside the coreFoundation String local variable. Since this is a Core Foundation object,ARC will not apply storage attributes to it, so we need to handle its memorymanually. Its retain count is 1, as with any newly created variable.

2. Then we typecast this Core Foundation string to a genericobject of type id. Note that wedidn't retain or release this object, so the retain count on both unknownOb jectType and coreFoundationString stays 1. Wesimply typecasted it to an object of type id.

3. Now we are retaining the generic object of type id and placing the resultingobject into another Core Foundation string. At this time, the retain count onthe core FoundationString, unknownObjectType, and anotherString variables is 2 and all three of these variables point tothe same location in the memory.

1中的apply storage attributes to it,额,是不是上面我拉了点东西啊。导致我后面就不理解了。

4的(because of the strong NSStringvariable, shooting the retain count from 1 to 2again

这句话应该就是1中的那个apply storage attributes to it意思吧,反正456就看不懂了,

4. What we are doing after that is to assign the valueinside coreFoundationString to a strong local NSStringusing the __bridge_transfer typecasting option. This will make sure that the coreFoundationString object willget released after this assign- ment (the retain count will go from 2 to 1) andit will again be retained (because of the strong NSStringvariable, shooting the retain count from 1 to 2again) So now coreFoundationString, unknownObjectType,anotherString and the objCString var- iables all point tothe same string with the retain count of 2.

5. Next stop, we are setting our strong local variable objCString to nil. This will release thisvariable and our string's retain count will go back to 1. All these localvariables are still valid and you can read from them because the retain countof the string that all of them point to is still 1.

6. Then we are explicitly releasing the value in the anotherString variable. This will setthe release count of our object from 1 to 0 and our string object will getdeal- located. At this point, you should not use any of these local variablesbecause they are pointing to a deallocated object—except for the objCString strong local vari- able,whose value was set to nil by us.

回去看了一下上面那一章,__strong NSString *yourString = @"Your String";,是不是省略了这个__strong,这个意思。日后再补补吧。

1.18 Delegating Tasks with Protocols

不看了,影响夫人休息,睡了。2012/10/2  11.07

2012/10/3 7.26,起床洗漱完毕,等早饭中,继续,这部分内容,本来就知道了,所以看起来应该会比较简单。

这里有一段话,个人认为写的挺好的,写的很透彻。

Cocoa Touch has given protocols a really nice meaning inObjective-C. In Cocoa Touch, protocols are the perfect means for definingdelegateobjects. A delegate object is an object that another object consults whensomething happens in that object. For instance, your repair man is yourdelegate if something happens to your car. If some- thing happens to your car,you go to yoru repair man and ask him to fix the car for you (although some preferto repair the car themselves, in which case, they are their own delegate fortheir car). So in Cocoa Touch, many classes expect a delegate object and makesure that whatever object is assigned as their delegate conform to a certainpro- tocol.

 

看到1.19

先介绍了两个概念

Base SDK

The SDK that you use to compile your application. Thiscan be the latest and the greatest SDK with access to all the new APIsavailable in iOS SDK.

Deployment SDK/Target

This is the SDK that will be used when you compile yourapp to run on devices.

这个是方法1:

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects: @"Item 1",

@"Item 4", @"Item 2", @"Item5", @"Item 3", nil];

NSLog(@"Array = %@", array);
if ([NSArrayinstancesRespondToSelector:@selector(sortUsingComparator:)]){

/* Use the sortUsingComparator: instance method of thearray to sort it */

}
else if ([NSArray instancesRespondToSelector:

@selector(sortUsingFunction:context:)]){

/* Use the sortUsingFunction:context: instance method ofthe array to sort */

}
else {

/* Do something else */ }

方法2:

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:

@"Item 1", @"Item 4", @"Item2", @"Item 5", @"Item 3", nil];

NSLog(@"Array = %@", array);
if ([arrayrespondsToSelector:@selector(sortUsingComparator:)]){

/* Use the sortUsingComparator: instance method of thearray to sort it */

}
else if ([arrayrespondsToSelector:@selector(sortUsingFunction:context:)]){

/* Use the sortUsingFunction:context: instance method ofthe array to sort */

}
else {

/* Do something else */ }

1.20

Use the NSClassFromString function. Pass the name of your class to this method as astring. If the return value of this function is nil, that class isnot available on the device thatruns your app; otherwise, that class is available on the device and you can goahead and use it as you wish. Here is an example:

if (NSClassFromString(@"NSJSONSerialization")!= nil){

/* You can use this class */
[NSJSONSerializationJSONObjectWithData:... /* Put data here */

options:... /* Put options here */ error:...]; /* Handleerrors here */

} else {
/* That class is not available */

}

string 之类的就直接跳过了。

直接跳到1.26 bundle,貌似以前看过。

从亲戚家玩了一天回来,丈母娘在工作和老婆大人都在学习,我也把电脑拿出来看书了。

看完1.26,我继续1.27.

1.28,这一小结草草过了,呵呵,打算有时间试一下。

看到1.29消息中心。最后这个有点懒得看了,也晚了,都9.44了。

第二章是讲控件的,我怎么也想跳过去了啊。

现在试一下,bundle。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值