0. id

id is a pointer to any type, but unlike void* it always points to an Objective-C object. For example, you can add anything of type id to an NSArray, but those objects must respond to retain and release.

The compiler is totally happy for you to implicitly cast any object to id, and for you to cast id to any object. This is unlike any other implicit casting in Objective-C, and is the basis for most container types in Cocoa.

1. weak Assign

  1. __weak, weak不会让ARC的引用记数+1, 对象被销毁时指针被清空为nil

  2. __unsafe_unretained, ARC不计数不+1, 但对象被销毁时指针不会nil

2. frame && bound

  1. frame 以父控件左上角为原点。bounds以自己为原点。

  2. frame 和 bounds都用来描述一块区域,

frame描述可视范围,how large we can see

bounds, how large we can see in the content

bounds描述可视范围在内容中的区域 所有subview都是相对于内容的,修改bounds本质是修改内容区域的原点;

Since frame relates a view's location in its parent view, you use it when you are making outward changes, like changing its width or finding the distance between the view and the top of its parent view.

Use the bounds when you are making inward changes, like drawing things or arranging subviews within the view. Also use the bounds to get the size of the view if you have done some transfomation on it.

3. bool 不需要设置strong or weak,

because it’s primitive types. 还有struct, enum也是,只要不是oc对象的都是

4. 性别用枚举 ??

typedef struct time{
int date;
int month;
} myData;

typedef enum sex{

male,

female

} mySex;

@property (nonatomic, assign) mySex sex;

5. @class

0), #include C language

1). #import,如果头文件改变,所有import头文件的类都要重新编译, 同样文件只import 一次

2). @class, 只是告诉编译器是一个类,不会检测实际内容,所以不需要重新编译

好处:不用重新编译, 避免循环引用

6. @autoreleasepool

When object instance is marked as autoreleased, it will have a retain count of +1 at that moment in time.

At the end of the run loop, the pool is drained, and any object marked autorelease then has its retain count decremented.

It's a way of keeping an object around while you prepare whatever will retain it for itself.

不适用于大内存

Extend tmp variable's life time

7. instancetype and id

instancetype 可以智能判断返回的类型和接受的类型是否一致,也只能当做返回值

8. ARC

只要对象没有被强指针指向就会被释放

__weak Person* person // weak pointer

9. Category, (Swift's extension)

声明 -> 实现 -> 使用

Person(Base) 分类可以访问原来类中的实例变量

不可以在分类中定义变量, 但是可以利用runtime 定义variables

An informal protocol is a Category on NSObject or its subClass under

NSFoundation

10. Extension VS Category

  • Extension(compile time): the compiler will expect you to implement the methods within your main@implementation. An extension is best for private methods which you would like to declare in your.mfile. I use it for this all the time.

    extensions can add extra instance variables to a class,

  • Category(run time): you have a separate@implementation block. Categories are more useful when you want to group your methods into different sections — categories — or when you want to add code to existing classes that you didn't create

  • Caution: use an extension at the top of your main.m file it's meant to be just that, an extension.

anonymous Category, 是有头文件。不是必须实现,可添加变量。实现只能在当前类中。

@interface Person()
@end

作用

定义类的私有变量和方法

不是绝对私有,当一个类调用这个类的头文件,这个类的对象也能访问

11. Block

12. Protocol

就是一个头文件。 一堆方法声明。没有成员变量。

使用流程:声明,引用,采纳(导入头文件),

//declare
@protocol protocolName <NSObject>
@require
// xxx

@optional
// xxx
@end;

//adapt
@interface className: NSObject<protocolName>

协议可以继承协议,所有方法都要被实现

#import "ProtocolA.h"

@protocol ProtocolB <ProtocolA, NSObject>
@end

类型限制

格式:id <协议名称> 变量名

//下面写法表示,赋值给girlFriend的对象必须采纳了houseHold协议
//Person 也必须导入并遵守这个协议
//如果不满足,则给出警告
id<houseHold> girlFriend = [[Person alloc] init];

//只能将遵守household协议的对象传给girlFriend
Person<HouseHold>*p = [Person alloc];

13. 初始化

//初始化的是类的属性
- (instancetype)initWithPerson:(NSString *)name {
    if (self = [super init]) {
        self.name = name;
    }
    return self;
}

// auto override init
- (instancetype)init {
    self = [super init];
    if (self != nil) {
        self.name = @"DefaultName";
    }
    return self;
}

// combine
- (instancetype)init {
    self = [self initWithPerson: @"DefaultName"];
    return self;
}

14. 代理模式 Delegation Pattern

谁做事,谁就是代理人 (UITableViewController)

who implements things, who is delegator.

谁指派事情, 谁是被代理(UITableView)

who assigns things, who is delegated

15. super, superclass, class

  • class: 获取当前类

  • superClass: 获取当前父类

  • super: 只是个编译指示器,与self类似,不同的是会用当前对象调父类方法

Person

SubPerson:Person
NSLog(@"%@, %@, %@, %@", 
    [self class], 
    [self superClass], 
    [super class], // super indicator is still the self  
    [super superClass] // super indicator is still the self
);
subPerson, Person, subPerson, Person

16. Static && extern

  • static修饰全局变量, 只能在该文件内使用 如果不加static, 外部可以通过extern int a 来访问(只要是全局变量, 可以在任何地方被访问)

  • static修饰局部变量,

    • 会延长生命周期。程序结束才销毁

    • 只会初始化一次,分配一次内存(程序已开始就分配)。

void test() {
    static int a = 0;
    a++;
    NSlog(@"%d,",a);
}
test();
test();
test();
//1, 2, 3

extern 只能用来声明外部全局变量,不能用于定义。 作用原理,先从当前文件找, 在从外部文件找

17. Const 与 Macro define区别

Macro:

  • 编译时刻不一样:Macro预编译, Const编译

  • const 编译type检查,Macro没有检查

  • macro 可以定义method(), const不可以

  • 大量使用Macro,会让预编译过长

Const:

  • 只能修饰基本变量活着指针变量, int a, int *p

  • 被Const 修饰的变量,只读

int * const p; // p: read only, *p: var   const pointer to int
int const * p1; // p1: var, *p1: var  pointer to const int    
const int * p2; // p2: var, *p2: var  pointer to const int
const int const *p3; // p2: read only, *p2: read only, const pointer to const int
int const * const p4; // p4: read only, *p4: read only,  

// const int * == int const *
// const int * const == int const * const

NSString* const name = @"xxx"; //不让谁改就修饰谁

static NSString* const x = @"xxx";

规定: 全局变量不能定义在类中,因为分散定义会导致重复定义错误。

解决:自己创建一个全局文件管理全局变量Globals。然后用extern定义即可。导

入.h。

@property GlobalConst:NSObject extern NSString* const name; 
@end

@implementation GlobalConst:NSObject NSString* const name = @"xxx"; 
@end

18 Copy ???

set方法

_name = [name copy];

19. Size vs Center

  1. 如果size从frame中取出,则要先设置size,在设置center

  2. bounds没问题

20. Notification 的顺序

一定要先观察再调用

21. Notification 多线程

  • 接受消息的函数执行的线程根发送通知的线程一致

  • 所以在接受消息的函数中用主队列来更新UI

22. 位移枚举

Enum 1:

typedef enum {
    FBTypeTop,
    FBTypeLeft,
    FBTypeRight,
    FBTypeDown,
} FBType;

Enum 2:

//可定义类型。
typedef NS_ENUM(NSInteger, FBType) {
    FBTypeTop,
    FBTypeDown,
    FBTypeLeft,
    FBTypeRight,
}
//使用:一个参数只能传一个值

Enum 3: ???

//可定义类型。可以多选,

typedef NS_ENUM(NSInteger, FBType) {
    FBTypeTop = 1<<0,
    FBTypeDown = 1<<1,
    FBTypeLeft = 1<<2,
    FBTypeRight = 1<<3,

}
//使用:一个参数可以传多个值

- (void) demo:(FBType)type {

if (type & FBTypeTop) {
        NSLog(@"tap top");
    }

if (type & FBTypeBotton) {

NSLog(@"both Top and Bottom");
    }

}


[self demo:FBTypeTop | FBTypeBotton];//执行两个

23. load() 和 initialize的区别

load(): 程序启动时调用,会把所有的类放进内存

initialize(): 第一次使用类的时候调用

----------------------------------------------------------------------------------------------

24. class extension

与 Category 的区别,括号里面有没有内容

@interface ClassName()
...
@end

// 文档注释
/**注释*/

25. lazy load

用的时候再加载, 且只加载一次, 本质上是修改get方法 还有一个好处:

didunload() 数据任意时刻被意外销毁,下次调用仍然会被创建

@interface XX
@property(strong, nonatomic)NSArray*shops;
@end

默认:

- (NSArray*) shops {
    return _shops;
}

修改:

- (NSArray*) shops {
    if (_shop == nil) {
        NSString *file = [[NSBundle main] pathForResrouce:..@"plist"];
        _shops = [NSArray arrayWithContentFile:file];
    }
    return _shops;
}

26. Property setter and getter 方法

@property (nonatomic, copy)NSString* name;
- (void) setName: (NSString*)name {
    _name = name;
}
- (NSString*)name {
    return _name;
}

27. _xxx 和 self.xxx

_xxx直接访问地址,不会掉用setter和getter方法;

self.xxx 调用的是getter方法

一般init当中用_xxx

28. KVC

字典转模型

FBDeal* deal = [[self alloc] init];
FBDeal* deal = [[self alloc] init];
[deal setValueForKeysWithDectionary:dict];

29. CGRectGetMaxY

CGRectGetMaxY(View)
CGRectGetMinX方法的作用得到目前view在当前屏幕中相对于整个View的最小值(位于屏幕的最左边)
CGRectGetMaxX方法的作用得到目前view在当前屏幕中相对于整个View的最大值(位于屏幕的最右边)
CGRectGetMinY方法的作用得到目前view在当前屏幕中相对于整个View的最小值(位于屏幕的最上边)
CGRectGetMaxY方法的作用得到目前view在当前屏幕中相对于整个View的最大值(位于屏幕的最下边)

30. Notifacation发送通知

//NSNotification* note = [[NSNotification alloc] init];
- (void) handleNotification: (NSString*)name {

}

NSNotification* note = [NSNotification notificationWithName:@"xxx" object:self userInfo:@{@xx:xx, @xx:xx}];

[[NSNotificationCenter defaultCenter] postNotification:note];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(xxx:@"xxx") name:@"xxx" object:nil];

31. Delegate

真正干活的人

32. NS CF

一个是oc实现的,一个是c实现。NS面向对象。CF用在系统自带框架中。

NS&CF数据类型转换:bridge

  1. foundation -> CoreFoundation : __bridge_retained

  2. coreFoundation -> foundation : __bridge_transfer

NSString *str = @"xxx";
CFStringRef str2 = (__bridge CGStringRef)(str);

results matching ""

    No results matching ""