tips-2

作者: 沁晓Chr | 来源:发表于2015-12-22 19:10 被阅读31次

    Xcode release notes更新

    定义了泛型取出非id,而是定义的泛型类型可以用点语法(id不可用点语法)

    ...表示可以传任意个参数
    NSLog(@“”,…);
    objc_msgSet(id,SEL,…)

    ------------------------------RUNTIME----------------------
    // 使用运行时的第一步:导入<objc/message.h>
    // 第二步:Build Setting -> 搜索msg -> 设置属性为No
    // OC:运行时机制,消息机制是运行时机制最重要的机制
    // 消息机制:任何方法调用,本质都是发送消息

    // SEL:方法编号,根据方法编号就可以找到对应方法实现
    // [p performSelector:@selector(eat)];

    // 运行时,发送消息,谁做事情就那谁
    // xcode5之后,苹果不建议使用底层方法
    // xcode5之后,使用运行时.

    // 让p发送消息
    // objc_msgSend(p, @selector(eat));
    // objc_msgSend(p, @selector(run:),10);

    // 类名调用类方法,本质类名转换成类对象
    // [Person eat];
    // 获取类对象
    Class personClass = [Person class];

    // [personClass performSelector:@selector(eat)];

    // 运行时
    objc_msgSend(personClass, @selector(eat));

    _OBJC_MESSAGE_H

    OBJC_EXPORT id objc_msgSend(id self, SEL op, ...)
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

    遍历模型所有成员属性

    // ivar:成员属性
    // class_copyIvarList:把成员属性列表复制一份给你
    // Ivar *:指向Ivar(成员属性类型)指针
    // ivarList :指向一个成员变量数组
    // class:获取哪个类的成员属性列表
    // count:成员属性总数
    unsigned int count = 0;
    Ivar *ivarList = class_copyIvarList(self, &count);
    for ()
    {
    Ivar one=ivarList[i]//取出每一个成员属性(包括公开和私有)
    }
    // 获取成员名
    NSString *propertyName = [NSString stringWithUTF8String:ivar_getName(ivar)];

    // 成员属性类型
    NSString *propertyType = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];

    /**

    • Returns the name of an instance variable.
    • @param v The instance variable you want to enquire about.
    • @return A C string containing the instance variable's name.
      */
      OBJC_EXPORT const char *ivar_getName(Ivar v)
      __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

    /**

    • Returns the type string of an instance variable.
    • @param v The instance variable you want to enquire about.
    • @return A C string containing the instance variable's type encoding.
    • @note For possible values, see Objective-C Runtime Programming Guide > Type Encodings.
      */
      OBJC_EXPORT const char *ivar_getTypeEncoding(Ivar v)
      __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

    // 解决KVC报错

    //kvc没有找到和"key"对应的属性key时会来这个方法

    • (void)setValue:(id)value forUndefinedKey:(NSString *)key
      {
      if ([key isEqualToString:@"id"]) {
      _ID = [value integerValue];
      }
      // key:没有找到key
      // value:没有找到key对应的值
      NSLog(@"%@ %@",key,value);
      }

    分类中.h用property

    @interface NSObject (Objc)
    /**

    • 分类中用property,只作用到.h,也就是只会声明getter和setter方法不会在.m中生成对应的成员属性和方法实现.
      */
      @property (nonatomic, strong) NSString *name;

    @end

    分类中.h用property,.m实现联系,类似增加属性

    @implementation NSObject (Objc)

    • (void)setName:(NSString *)name
      {
      // 添加属性,跟对象
      // 给某个对象产生关联,添加属性
      // object:给哪个对象添加属性
      // key:属性名,根据key去获取关联的对象 ,void * == id
      // value:关联的值
      // policy:策略
      objc_setAssociatedObject(self, @"name", name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
      }

    • (NSString *)name
      {
      return objc_getAssociatedObject(self, @"name");
      }

    @end

    /**

    • Sets an associated value for a given object using a given key and association policy.
    • @param object The source object for the association.
    • @param key The key for the association.
    • @param value The value to associate with the key key for object. Pass nil to clear an existing association.
    • @param policy The policy for the association. For possible values, see “Associative Object Behaviors.”
    • @see objc_setAssociatedObject
    • @see objc_removeAssociatedObjects
      */
      OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
      __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);

    /**

    • Returns the value associated with a given object for a given key.
    • @param object The source object for the association.
    • @param key The key for the association.
    • @return The value associated with the key \e key for \e object.
    • @see objc_setAssociatedObject
      */
      OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)
      __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);

    当调用了没有实现的方法没有实现就会调用resolveInstanceMethod

    @implementation Person

    // 定义函数
    // 没有返回值,参数(id,SEL)
    // void(id,SEL)
    void aaaa(id self, SEL _cmd, id param1)
    {

    NSLog(@"调用eat %@ %@ %@",self,NSStringFromSelector(_cmd),param1);
    

    }

    // 默认一个方法都有两个参数,self,_cmd,隐式参数
    // self:方法调用者
    // _cmd:调用方法的编号

    // 动态添加方法,首先实现这个resolveInstanceMethod
    // resolveInstanceMethod调用:当调用了没有实现的方法没有实现就会调用resolveInstanceMethod
    // resolveInstanceMethod作用:就知道哪些方法没有实现,从而动态添加方法
    // sel:没有实现方法

    • (BOOL)resolveInstanceMethod:(SEL)sel
      {
      // NSLog(@"%@",NSStringFromSelector(sel));

      // 动态添加eat方法

      if (sel == @selector(eat:)) {

        /*
         cls:给哪个类添加方法
         SEL:添加方法的方法编号是什么
         IMP:方法实现,函数入口,函数名
         types:方法类型
         */
        // @:对象 :SEL
        class_addMethod(self, sel, (IMP)aaaa, "v@:@");
        
        // 处理完
        return YES;
      

      }

    return [super resolveInstanceMethod:sel];
    

    }
    @end

    交换方法实现,方法都是定义在类里面

    // 加载这个分类的时候调用

    • (void)load
      {

      // 交换方法实现,方法都是定义在类里面
      // class_getMethodImplementation:获取方法实现
      // class_getInstanceMethod:获取对象
      // class_getClassMethod:获取类方法
      // IMP:方法实现

      // imageNamed
      // Class:获取哪个类方法
      // SEL:获取方法编号,根据SEL就能去对应的类找方法
      Method imageNameMethod = class_getClassMethod([UIImage class], @selector(imageNamed:));

      // xmg_imageNamed
      Method xmg_imageNamedMethod = class_getClassMethod([UIImage class], @selector(xmg_imageNamed:));

      // 交换方法实现
      method_exchangeImplementations(imageNameMethod, xmg_imageNamedMethod);

    }

    ------------------------------RUNTIME----------------------

    A中static修饰全局变量,只能在A本文件中使用,没有修饰的全局变量,在B外可访问,访问时要用B中extern(外部的)声明,一下A中定义的全局变量,,如int a=2; extern只能声明不能定义,不能初始化.extern int a =2;//错的.extern只是声明a,说明a是其他文件的全局变量.extern翻译来自外部的.static修饰全局变量,缩小其使用范围,定义局部变量,扩展其生命周期

    A: int a=5;//A中定义全局变量
    B: extern int a;//B中声明a是来自外部的全局变量,然后就可以用了

    super 一个修饰标志,不是一个指针,实际上方法的调用者还是self.有点类似runtime中的交换方法实现

    这个是UIViewControl里面的属性

    @property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets NS_AVAILABLE_IOS(7_0); // Defaults to YES

    信号的绑定(其他的许多也是类似加工信号的原理,或拦截过滤)就是信号加工场
    要 接收原始信号,加工信号,发送加工信号

    define XMGLog(...) NSLog(VA_ARGS)

    …表示参数个数不定
    VA_ARGS表示要替代的参数.arguments

    相关文章

      网友评论

          本文标题:tips-2

          本文链接:https://www.haomeiwen.com/subject/wgxvhttx.html