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]; }
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 就会有缓存