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-;
[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]*;
//ios生成的时间戳是10位 13时间戳转为IOS的时间 NSString * timeStampString = @"";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStampString doubleValue] / ]; 或者 NSString * timeStampString = @"";
NSTimeInterval _interval=[[timeStampString substringToIndex:] 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
解决办法:
、系统推荐方法 [NSString stringWithFormat:@“%ld", (long)number]; 、强制转换 [NSString stringWithFormat:@"%d", (int)number]; 、[NSString stringWithFormat:@“%@", @(number)];
7:本地语言添加文件(解决一些系统自带的Title为英语 比如Cannel Done等)
:新建文件,Resource->Strings File 命名, :然后点击这个.strings 右边有个Localization->Localizeation,增加一个英语; :到Project-Info-Localizations 然后增加Chinses(Simplified) 选择.strings后就可以了
8:SDWebImage获得缓存大小,并对它进行清除
float tmpSize = [[SDImageCache sharedImageCache] getSize];
NSString *clearCacheName =@"当前缓存已清理";
if (tmpSize>) {
clearCacheName=tmpSize >= ? [NSString stringWithFormat:@"成功清理缓存(%.2fM)",tmpSize] : [NSString stringWithFormat:@"成功清理缓存(%.2fK)",tmpSize * ];
}
[[SDImageCache sharedImageCache] clearDisk];
说明:直接sd_setImageWithURL 就会有缓存
IOS开发基础知识--碎片28的更多相关文章
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- IOS开发基础知识--碎片33
1:AFNetworking状态栏网络请求效果 直接在AppDelegate里面didFinishLaunchingWithOptions进行设置 [[AFNetworkActivityIndicat ...
- IOS开发基础知识--碎片42
1:报thread 1:exc_bad_access(code=1,address=0x70********) 闪退 这种错误通常是内存管理的问题,一般是访问了已经释放的对象导致的,可以开启僵尸对象( ...
- IOS开发基础知识--碎片50
1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...
- IOS开发基础知识--碎片3
十二:判断设备 //设备名称 return [UIDevice currentDevice].name; //设备型号,只可得到是何设备,无法得到是第几代设备 return [UIDevice cur ...
- IOS开发基础知识--碎片11
1:AFNetwork判断网络状态 #import “AFNetworkActivityIndicatorManager.h" - (BOOL)application:(UIApplicat ...
- IOS开发基础知识--碎片14
1:ZIP文件压缩跟解压,使用ZipArchive 创建/添加一个zip包 ZipArchive* zipFile = [[ZipArchive alloc] init]; //次数得zipfilen ...
- IOS开发基础知识--碎片16
1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断对象类型 -(BOO ...
- IOS开发基础知识--碎片19
1:键盘事件顺序 UIKeyboardWillShowNotification // 键盘显示之前 UIKeyboardDidShowNotification // 键盘显示完成后 UIKeyboar ...
随机推荐
- exe4j的使用
下载:http://download.cnet.com/exe4j/3000-2070_4-144405.html 参考:http://blog.chinaunix.net/uid-25749806- ...
- 数据可视化-EChart2.0使用总结2
接上一篇博客,这篇博客主要讨论EChart里面的散点图.气泡图和雷达图. 4.散点图-Scatter Chart 适合场景:三维数据集,但是只有两个维度需要比较.比较的是X轴和Y轴的数据,第三个数 ...
- 使用MAT(Memory Analyzer Tool)工具分析dump文件--转
原文地址:http://gao-xianglong.iteye.com/blog/2173140?utm_source=tuicool&utm_medium=referral 前言 生产环境中 ...
- 【转】 个人认为,这是最详细的 android------HttpURLConnection 类用法详解。一些教材没讲到的,它讲到了
站在巨人的肩膀上,渐渐进步. 原文链接:http://www.blogjava.net/supercrsky/articles/247449.html 针对JDK中的URLConnection连接Se ...
- 软件工程-构建之法 理解C#一小段程序
一.前言 老师给出的要求: 阅读下面程序,请回答如下问题: 问题1:这个程序要找的是符合什么条件的数? 问题2:这样的数存在么?符合这一条件的最小的数是什么? 问题3:在电脑上运行这一程序,你估计多长 ...
- (1)从底层设计,探讨插件式GIS框架的实现
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 研一时,听当时的师兄推荐,买了蒋波涛的一本关于GIS插件框架的书.当时 ...
- 有了jsRender,妈妈再也不用担心我用jq拼接DOM拼接的一团糟了、页面整齐了、其他伙伴读代码也不那么费劲了
写在前面 说来也很巧, 下午再做一个页面,再普通不过的分页列表,我还是像往常一样,基于MVC环境下,我正常用PagedList.MVC AJAX做无刷新分页,这时候问题就来了,列表数据中有个轮播图用到 ...
- FineUI Grid控件高度自适应
引言 页面里使用f:Grid控件,添加分页功能,然后高度填充整个页面. 如何使用 使用FineUI 控件的每个页面都有一个f:PageManager控件,它包含属性:AutoSizePanelID,设 ...
- 用大白话聊聊JavaSE -- 自定义注解入门
注解在JavaSE中算是比较高级的一种用法了,为什么要学习注解,我想大概有以下几个原因: 1. 可以更深层次地学习Java,理解Java的思想. 2. 有了注解的基础,能够方便阅读各种框架的源码,比如 ...
- jQuery-1.9.1源码分析系列(五) 回调对象
jQuery.Callbacks()提供的回调函数队列管理本来是延时回调处理的一部分,但是后面将其独立出来作为一个模块.jQuery就是这样,各个模块间的代码耦合度是处理的比较好的,值得学习.虽然是从 ...