美文网首页iOS学习开发iOS开发iOS程序猿
[Xcode插件]Category添加属性后自动生成getter

[Xcode插件]Category添加属性后自动生成getter

作者: ame017 | 来源:发表于2018-04-17 15:29 被阅读49次

    AMECategoryMaker

    一个无需resign Xcode的category生成器

    ACM-introduce.png

    关于本扩展

    当你新建一个category并为他添加新的property之后,你必须手动实现getter和setter。
    安装这个插件会帮你自动实现这些
    例如:

    //copy
    @property (nonatomic, copy) NSString * view_version;
    //assign
    @property (nonatomic, assign) NSInteger numVersion;
    //strong
    @property (nonatomic, strong) UIButton * button;
    
    @property (atomic, strong) UIScrollView * scrollView;
    /*过滤注释*/
    //readOnly
    @property (nonatomic, readonly, copy) NSString * readOnlyString;
    //自动识别int float bool
    @property (nonatomic, assign) CGFloat float0;
    @property (nonatomic, assign) BOOL isAME;
    

    ↓↓↓

    static char * view_versionKey = "view_versionKey";
    static char * numVersionKey = "numVersionKey";
    static char * buttonKey = "buttonKey";
    static char * scrollViewKey = "scrollViewKey";
    static char * readOnlyStringKey = "readOnlyStringKey";
    static char * float0Key = "float0Key";
    static char * isAMEKey = "isAMEKey";
    
    - (void)setView_version:(NSString *)view_version{
        objc_setAssociatedObject(self, view_versionKey, view_version, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    - (NSString *)view_version{
        if(!objc_getAssociatedObject(self, view_versionKey)){
            NSString *object = [[NSString alloc]init];
            objc_setAssociatedObject(self, view_versionKey, object, OBJC_ASSOCIATION_COPY_NONATOMIC);
        }
        return objc_getAssociatedObject(self, view_versionKey);
    }
    
    - (void)setNumVersion:(NSInteger)numVersion{
        objc_setAssociatedObject(self, numVersionKey, @(numVersion), OBJC_ASSOCIATION_ASSIGN);
    }
    
    - (NSInteger)numVersion{
        return [objc_getAssociatedObject(self, numVersionKey)integerValue];
    }
    
    - (void)setButton:(UIButton *)button{
        objc_setAssociatedObject(self, buttonKey, button, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (UIButton *)button{
        if(!objc_getAssociatedObject(self, buttonKey)){
            UIButton *object = [[UIButton alloc]init];
            objc_setAssociatedObject(self, buttonKey, object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        }
        return objc_getAssociatedObject(self, buttonKey);
    }
    
    - (void)setScrollView:(UIScrollView *)scrollView{
        objc_setAssociatedObject(self, scrollViewKey, scrollView, OBJC_ASSOCIATION_RETAIN);
    }
    
    - (UIScrollView *)scrollView{
        if(!objc_getAssociatedObject(self, scrollViewKey)){
            UIScrollView *object = [[UIScrollView alloc]init];
            objc_setAssociatedObject(self, scrollViewKey, object, OBJC_ASSOCIATION_RETAIN);
        }
        return objc_getAssociatedObject(self, scrollViewKey);
    }
    
    - (NSString *)readOnlyString{
        if(!objc_getAssociatedObject(self, readOnlyStringKey)){
            NSString *object = [[NSString alloc]init];
            objc_setAssociatedObject(self, readOnlyStringKey, object, OBJC_ASSOCIATION_COPY_NONATOMIC);
        }
        return objc_getAssociatedObject(self, readOnlyStringKey);
    }
    
    - (void)setFloat0:(CGFloat)float0{
        objc_setAssociatedObject(self, float0Key, @(float0), OBJC_ASSOCIATION_ASSIGN);
    }
    
    - (CGFloat)float0{
        return [objc_getAssociatedObject(self, float0Key)doubleValue];
    }
    
    - (void)setIsAME:(BOOL)isAME{
        objc_setAssociatedObject(self, isAMEKey, @(isAME), OBJC_ASSOCIATION_ASSIGN);
    }
    
    - (BOOL)isAME{
        return [objc_getAssociatedObject(self, isAMEKey)boolValue];
    }
    
    category-gif.gif

    安装方法

    Xcode8.0+

    1. 点我下载本扩展
    2. 在系统偏好设置-扩展中打开本插件(如图)

      extensionUse.png
    3. 你可以自己设置一个快捷键

      binding.png

    一些提示

    如果您的Xcode版本是 8.0+但是无法使用

    请安装macOS Sierra (version 10.12)

    相关文章

      网友评论

      • 红枫叶HM:这个我好像用过,就是老是会失效,可能是重启电脑之后,可能是xcode重启之后,用了几次都失效了,就不用了
        ame017:@红枫叶HM 不会失效的啊.我有两个这样的插件 都运行良好
        红枫叶HM:@ame017 哦,那就是类似的,也是在设置里面配置,然后xcode添加快捷键,你可以多测试测试,看会不会失效吧,如果不失效,那确实用着方便
        ame017:这个是我昨天刚写的呀0.0

      本文标题:[Xcode插件]Category添加属性后自动生成getter

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