(杂记)开发小计

308 阅读1分钟

RAC如何观察NSMutable

NSMutableArray *fromKVC =[self mutableArrayValueForKey:@"postedImagesIds"];
[[self mutableArrayValueForKey:@keypath(self.datas)] addObjectsFromArray:value];

获取动态 void *

static void *CoustomeContextOrKey  = &CoustomeContextOrKey;
static private var CoustomeContextOrKey: Void?

KVO自动观察

在需要的类里面复写下面的方法,用来关闭自动观察

+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key {
    // 添加自定义操作
    return NO;
}

在需要KVO的地方手动添加willChangedidChange 用来省去需求更改忘记或者多添加了删除KVO操作所带来的Bug。

复合式KVO

伪代码 Model.m

+ (NSSet<NSString *> *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
    NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
    if([key isEqualToString:@"complexValue"]) {
        NSArray *affectingKeys = @[@"subValue1", @"subValue2"];
        keypaths = [keyPaths setByAddingObjectsFromArray:affectingKeys];
    }
    return keyPaths;
}

+ (NSString *)ComplexValue {
    return subValue1+subValue2;
}
 

objc_msgSend / objc_msgSendSuper发送参数

void (*sq_msgSendSuper)(void *,SEL , id) = (void *)objc_msgSendSuper;
    // void /* struct objc_super *super, SEL op, ... */
    struct objc_super superStruct = {
        .receiver = self,
        .super_class = class_getSuperclass(object_getClass(self)),
    };
    //objc_msgSendSuper(&superStruct,_cmd,newValue)
    sq_msgSendSuper(&superStruct,_cmd,newValue);

NSOperation

- (BOOL)isConcurrent {
    return YES;
}

Clang编译

clang -rewrite-objc main.c -o main.cpp

git找回

git branch -avv

git reflog

git reset --hard 718f8b901

rn库更新

npm install

主动触发View所在的ViewController的viewDidLayoutSubviews方法

override func layoutSubviews() {
        super.layoutSubviews()
        
        /// 用于触发viewController的`viewDidLayoutSubviews`
        setNeedsUpdateConstraints()
 }