Objective-C-语法积累

1.#import <Foundation/Foundation.h>

这里 Foundation 指的是NSObject 的父类,Foundation.h是框架 

 

2.Nonatomic means calling this setter and getter that go along with this property is not thread safe.

 

非原子是说同该属性一同的settergetter不是线程安全的

 

So you can't have two threads trying to set this property at the same time.

 

你不能有两个线程 同时尝试设置该属性

 

Why do we say nonatomic here, why do we want this to not be thread safe?

 

为什么这里要非原子呢 为什么不能是线程安全的呢

 

Because the way we do multithreading in iOS is not by having a single object that multiple threads are setting on,we usually have a separate set of objects that are running in another thread,like your model,and then other, then your UI stuff is running in the UI threadand they're talking thread to thread.

 

因为iOS中的多线程,不是一个对象上多个线程在设置,我们通常有另外一组对象 运行在另外一个线程例如你的模型,然后UI这些东西运行在UI线程,它们在线程间进行通信

 

So we don't need this and not only that,

 

所以我们不需要这个 不仅如此

 

what's going to happen here when we do this at sign property is objective-C is going to create that getter and setter methods that I told you about to set these contents automatically for us,

 

这里使用@propertyObjective-C会创建我讲过的gettersetter方法来自动为我们设置contents

 

If we don't say nonatomic, there's going to be all kinds of locking code in there, right?

 

如果我们不说nonatomic 这里会有各种锁定代码(我们不想要锁定代码)

 

 

 

 

 

- (NSString *)contents {

return _contents;

}

- (void)setContents:(NSString *)contents {

_contents = contents;

}

 

这里是getter 和 setter 方法 ,一般不用自己实现,是Objective-c自动实现好的。

 

3.BOOL没有强弱引用,它是原始类型,不在堆中。

 

4.这里可以修改默认的getter方法的名字

@property (nonatomicgetter=isChosen) BOOL chosen;

 

- (BOOL)isChosen {

return _chosen;

}

 

 

 

5.注意:在局部不使用NSNumber 使用NSNumber通常是为了将其传递给方法哪怕这时你也可以传递intfloat

 

6.在真正要用到的时候在实例化,叫做惰性实例化方法。

 

7.

这里这个函数用来实现判断suit 是否在这个数组中的四个花色里面


8.readwrite 只用在重新声明公共中的只读为私有中的读写时(这是我们使用这个的唯一情况)

例如:

@property (nonatomicreadonlyNSInteger score;

@property (nonatomicreadwriteNSInteger score;

 

 

9.控制器的首要任务之一:同步模型和UI

10.otherCards[0]在数组为空时会崩溃,导致数组下标越界错误,firstObject 还有lastObject它们有这种避免数组下标越界的额外功效,firstObject会返回第一个对象或nil

11.如果你调用一个返回struct的方法,你要检验 确保没有将它发送给nil

12.动态绑定:id是一个指针,id的用处:在view 与 controller的盲目结构化通信过程中你需要有指针指向类型未知的对象

13.id方面 我们有两大方法来保护自己:

1)一是内省(introspection),也就是我们可以在运行时问一个id 你是什么类的,或你对什么方法作出反应,这是一种很棒的保护方式

isKindOfClass② ③respondsToSelector

(2)另一种方式叫作协议,协议是在id后使用尖括号来表示,(这是一个id 一个指针 指向的对象我不知道类是什么,但它要能对尖括号中定义的这一组方法作出反应,这就是我们进行委托和数据源这些的方式)

14.selector可以说是一个方法名的识别器,@selector(方法名

  SEL shootSelecter = @selectershoot;(注意,SEL是方法的类型

15.NSDictionary的用法:

 

 

16.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值