objective-c技巧,细节

1.绝对值函数abs(),可用于求两个数值之间的差距

2.如果代码如下:

 if (some condition) {

  something = YES;

  } else {

  something = NO;

  } 

则,可用something = (some condition); 代替

 3.用Properties的好处有两个:

  (1)方便,省代码:the compiler automatically adds the setText and text methods to your object, as well as the instance variable that will hold the     value for this property. Now you can simply use dot syntax to read and write the value of text. 

   (2)符合面向对象的封装性:when you put properties in the @interface to make data available to others,you use properties these other objects never     directly access your internal instance variables, they always go through a getter or setter method first. 

 4.id类型常用的地方:

  You’ll often use id to describe an object that conforms to a specific protocol, without having to know anything about its class. That’s what you’ve been doing for your delegates all along: 

  @property (nonatomic,weak) id <ItemDetailViewControllerDelegate> delegate;

  The notation id <ItemDetailViewControllerDelegate> means: “I only care that this object implements the ItemDetailViewControllerDelegate protocol but its actual class is not important to me.” 

  该delegate指向实现了ItemDetailViewControllerDelegate协议方法的对象(视图控制器),但delegate所在的对象(视图控制器)除了知道那个对象实现了协议方法外,不需要知道任何其他东西

posted @ 2016-04-29 12:24  XuDeHong  阅读(146)  评论(0编辑  收藏  举报