iOS小笔记本

2017-07-06  本文已影响0人  rapunzelyeah

strong和weak

KVO

 - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath 
options:(NSKeyValueObservingOptions)options 
context:(void *)context;

接收变更通知

 - (void)observeValueForKeyPath:(NSString *)keyPath 
ofObject:(id)object change:(NSDictionary *)change   context:(void *)context;

移除对象的观察者身份

 - (void)removeObserver:(NSObject *)observer 
forKeyPath:(NSString *)keyPath;

KVO中谁要监听谁注册,然后对响应进行处理,使得观察者与被观察者完全解耦。KVO只检测类中的属性,并且属性名都是通过NSString来查找,编译器不会检错和补全,全部取决于自己。

堆和栈

UIViewController的生命周期

-[ViewController initWithNibName:bundle:];
-[ViewController init];
-[ViewController loadView];
-[ViewController viewDidLoad];
-[ViewController viewWillAppear:];
-[ViewController viewWillLayoutSubviews:];
-[ViewController viewDidLayoutSubviews:];
-[ViewController viewDidAppear:];
-[ViewController viewWillDisappear:];
-[ViewController viewDidDisappear:];
-[ViewController viewWillUnload:];
-[ViewController viewDidUnload:];

CRC8校验

文档提供的代码为:

static U8 CRC8(U8 *ptr, U8 len)
{
    U8 crc;
    U8 i;
    crc = 0;
    while(len--)
    {
        crc ^= *ptr++;
        for(i = 0; i < 8; i++)
        {
            if(crc & 0x01)
            {
                crc = (crc >> 1) ^ 0x8c;
            }
            else {
                crc >>= 1;
            }
        }
    }
    return crc;
}

转换为iOS代码为:

- (Byte)CRC8Result:(Byte *)bytes length:(NSInteger)length {
    Byte crc = 0x00;
    for (int k = 0; k < length - 1; k++) {
        crc ^= bytes[k];
        for (int i = 0; i < 8; i++) {
            if (crc & 0x01) {
                crc = (crc >> 1) ^ 0x8c;
            } else {
                crc >>= 1;
            }
        }
    }
    NSLog(@"crc校验码%@", [NSString stringWithFormat:@"%0.2hhx", (char)crc]);
    return crc;
}
上一篇下一篇

猜你喜欢

热点阅读