zoukankan      html  css  js  c++  java
  • IOS开发基础知识--碎片28

    1:通用的weakify和strongify

    /**
    * 强弱引用转换,用于解决代码块(block)与强引用self之间的循环引用问题
    * 调用方式: `@weakify_self`实现弱引用转换,`@strongify_self`实现强引用转换
    *
    * 示例:
    * @weakify_self
    * [obj block:^{
    * @strongify_self
    * self.property = something;
    * }];
    */
    #ifndef    weakify_self
    #if __has_feature(objc_arc)
    #define weakify_self autoreleasepool{} __weak __typeof__(self) weakSelf = self;
    #else
    #define weakify_self autoreleasepool{} __block __typeof__(self) blockSelf = self;
    #endif
    #endif
    #ifndef    strongify_self
    #if __has_feature(objc_arc)
    #define strongify_self try{} @finally{} __typeof__(weakSelf) self = weakSelf;
    #else
    #define strongify_self try{} @finally{} __typeof__(blockSelf) self = blockSelf;
    #endif
    #endif
    /**
    * 强弱引用转换,用于解决代码块(block)与强引用对象之间的循环引用问题
    * 调用方式: `@weakify(object)`实现弱引用转换,`@strongify(object)`实现强引用转换
    *
    * 示例:
    * @weakify(object)
    * [obj block:^{
    * @strongify(object)
    * strong_object = something;
    * }];
    */
    #ifndef    weakify
    #if __has_feature(objc_arc)
    #define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
    #else
    #define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
    #endif
    #endif
    #ifndef    strongify
    #if __has_feature(objc_arc)
    #define strongify(object) try{} @finally{} __typeof__(object) strong##_##object = weak##_##object;
    #else
    #define strongify(object) try{} @finally{} __typeof__(object) strong##_##object = block##_##object;
    #endif
    #endif

     运用(这两个宏一定成对出现,先weak再strong):

    @weakify(self); // 定义了一个__weak的self_weak_变量 
    [RACObserve(self, name) subscribeNext:^(NSString *name) { 
        @strongify(self); // 局域定义了一个__strong的self指针指向self_weak 
        self.outputLabel.text = name; 
    }]; 

     

    2:objc runtime 动态增加属性

    说明:给类扩展增加了一个新属性。通常下类扩展只允许添加方法。必须要先引入 objc/runtime.h,主要是下面两个方法:

    赋值:
    
    OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
    
    获得值:
    
    OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)

    实例(引用):

    UILabel+Associate.h
    
    
    #import <UIKit/UIKit.h>
    
    @interface UILabel (Associate)
    
    - (void) setFlashColor:(UIColor *) flashColor;
    
    - (UIColor *) getFlashColor;
    
    @end
    
    
    
    UILabel+Associate.m
    
    
    #import "UILabel+Associate.h"
    #import <objc/runtime.h>
    
    @implementation UILabel (Associate)
    
    static char flashColorKey;
    
    - (void) setFlashColor:(UIColor *) flashColor{
        objc_setAssociatedObject(self, &flashColorKey, flashColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (UIColor *) getFlashColor{
       return objc_getAssociatedObject(self, &flashColorKey);
    }
    
    @end

    调用代码:

        UILabel *lab = [[UILabel alloc] init];
        [lab setFlashColor:[UIColor redColor]];
        NSLog(@"%@", [lab getFlashColor]);

     

    3:navigationController popToViewController跳转到上上层

            NSUInteger index=self.navigationController.viewControllers.count-3;
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index] animated:YES];

    如果动画是翻转页面,有可能是因为当前页面有键盘,可以先把键盘回收后,动作就变成正常的回退动画效果

    4:App跳转到设置

                 UIApplication *app = [UIApplication sharedApplication];
                 NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                 if ([app canOpenURL:settingsURL]) {
                     [app openURL:settingsURL];
                 }
    跳转系统设置根目录中的项目使用如下的方法:
    
             _array = @[
                   @{@"系统设置":@"prefs:root=INTERNET_TETHERING"},
                   @{@"WIFI设置":@"prefs:root=WIFI"},
                   @{@"蓝牙设置":@"prefs:root=Bluetooth"},
                   @{@"系统通知":@"prefs:root=NOTIFICATIONS_ID"},
                   @{@"通用设置":@"prefs:root=General"},
                   @{@"显示设置":@"prefs:root=DISPLAY&BRIGHTNESS"},
                   @{@"壁纸设置":@"prefs:root=Wallpaper"},
                   @{@"声音设置":@"prefs:root=Sounds"},
                   @{@"隐私设置":@"prefs:root=privacy"},
                   @{@"APP Store":@"prefs:root=STORE"},
                   @{@"Notes":@"prefs:root=NOTES"},
                   @{@"Safari":@"prefs:root=Safari"},
                   @{@"Music":@"prefs:root=MUSIC"},
                   @{@"photo":@"prefs:root=Photos"}
                   ];
            NSURL * url = [NSURL URLWithString:[_array[index] allValues].firstObject];
            [[UIApplication sharedApplication]openURL:url];
    如果要跳转第三方应用的设置界面中,使用prefs:root=boundleId的方式,boundleId是第三方应用的boundleId。
            如果需要继续向项目内层进行跳转,可以通过添加path路径的方式,如下:
    
                  _array = @[
                   @{@"关于本机":@"prefs:root=General&path=About"},
                   @{@"软件升级":@"prefs:root=General&path=SOFTWARE_UPDATE_LINK"},
                   @{@"日期时间":@"prefs:root=General&path=DATE_AND_TIME"},
                   @{@"Accessibility":@"prefs:root=General&path=ACCESSIBILITY"},
                   @{@"键盘设置":@"prefs:root=General&path=Keyboard"},
                   @{@"VPN":@"prefs:root=General&path=VPN"},
                   @{@"壁纸设置":@"prefs:root=Wallpaper"},
                   @{@"声音设置":@"prefs:root=Sounds"},
                   @{@"隐私设置":@"prefs:root=privacy"},
                   @{@"APP Store":@"prefs:root=STORE"},
                   @{@"还原设置":@"prefs:root=General&path=Reset"},
                   @{@"应用通知":@"prefs:root=NOTIFICATIONS_ID&path=应用的boundleId"}
                   ];

    5:ios时间戳13位转换

    IOS的时间转为13位时间戳
    
    //取当前时间的秒数,这边是到秒数
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    //到毫秒数,则再*1000
    long long curTime=[[NSDate date] timeIntervalSince1970]*1000;
    //ios生成的时间戳是10位
    
    
    13时间戳转为IOS的时间
    
    NSString * timeStampString = @"1423189125874";
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStampString doubleValue] / 1000];
    
    或者
    
    NSString * timeStampString = @"1423189125874";
       NSTimeInterval _interval=[[timeStampString substringToIndex:10] doubleValue];
       NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];

     

    6:iOS之整型(NSInteger)转换警告Values of type 'NSInteger' should not be used as format arguments;

    苹果app支持arm64以后会有一个问题:NSInteger变成64位了,和原来的int (%d)不匹配,会报如下warning,

    Values of type 'NSInteger' should not be used as format arguments; add an explicit cast to 'long' instead

    解决办法:
    1、系统推荐方法   [NSString stringWithFormat:@“%ld", (long)number];
    
    2、强制转换    [NSString stringWithFormat:@"%d", (int)number];
    
    3、[NSString stringWithFormat:@“%@", @(number)];

    7:本地语言添加文件(解决一些系统自带的Title为英语 比如Cannel Done等)

    1:新建文件,Resource->Strings File 命名,
    
    2:然后点击这个.strings 右边有个Localization->Localizeation,增加一个英语;
    
    3:到Project-Info-Localizations 然后增加Chinses(Simplified) 选择.strings后就可以了

    8:SDWebImage获得缓存大小,并对它进行清除

            float tmpSize = [[SDImageCache sharedImageCache] getSize];
            NSString *clearCacheName =@"当前缓存已清理";
            if (tmpSize>0) {
                clearCacheName=tmpSize >= 1 ? [NSString stringWithFormat:@"成功清理缓存(%.2fM)",tmpSize] : [NSString stringWithFormat:@"成功清理缓存(%.2fK)",tmpSize * 1024];
            }
            [[SDImageCache sharedImageCache] clearDisk];

    说明:直接sd_setImageWithURL 就会有缓存

  • 相关阅读:
    ASP.NET vs MVC vs WebForms
    asp.net web forms和asp.net mvc比较
    cxx11emu.h 和 logprint.h
    获取代码中宏定义等信息的一些手段
    openwrt luci web分析
    QSDK与OPENWRT区别
    OpenWrt 中查看 Flash RAM CPU 信息
    深入剖析Linux IO原理和几种零拷贝机制的实现
    Linux ass2srt
    bsd pkg install gcc gmake cmake gdb cgdb
  • 原文地址:https://www.cnblogs.com/wujy/p/4952267.html
Copyright © 2011-2022 走看看