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的更多相关文章

  1. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  2. IOS开发基础知识--碎片33

    1:AFNetworking状态栏网络请求效果 直接在AppDelegate里面didFinishLaunchingWithOptions进行设置 [[AFNetworkActivityIndicat ...

  3. IOS开发基础知识--碎片42

    1:报thread 1:exc_bad_access(code=1,address=0x70********) 闪退 这种错误通常是内存管理的问题,一般是访问了已经释放的对象导致的,可以开启僵尸对象( ...

  4. IOS开发基础知识--碎片50

      1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...

  5. IOS开发基础知识--碎片3

    十二:判断设备 //设备名称 return [UIDevice currentDevice].name; //设备型号,只可得到是何设备,无法得到是第几代设备 return [UIDevice cur ...

  6. IOS开发基础知识--碎片11

    1:AFNetwork判断网络状态 #import “AFNetworkActivityIndicatorManager.h" - (BOOL)application:(UIApplicat ...

  7. IOS开发基础知识--碎片14

    1:ZIP文件压缩跟解压,使用ZipArchive 创建/添加一个zip包 ZipArchive* zipFile = [[ZipArchive alloc] init]; //次数得zipfilen ...

  8. IOS开发基础知识--碎片16

    1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断对象类型 -(BOO ...

  9. IOS开发基础知识--碎片19

    1:键盘事件顺序 UIKeyboardWillShowNotification // 键盘显示之前 UIKeyboardDidShowNotification // 键盘显示完成后 UIKeyboar ...

随机推荐

  1. c++ stringstream(老好用了)

    前言: 以前没有接触过stringstream这个类的时候,常用的字符串和数字转换函数就是sscanf和sprintf函数.开始的时候就觉得这两个函数应经很叼了,但是毕竟是属于c的.c++中引入了流的 ...

  2. Windows下使用Dev-C++开发基于pthread.h的多线程程序

    一.下载Windows版本的pthread 目前最新版本是:pthreads-w32-2-9-1-release.zip. 二.解压pthread到指定目录      我选择的目录是:E:\DEV-C ...

  3. .NET平台开源项目速览(6)FluentValidation验证组件介绍与入门(一)

    在文章:这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,给大家初步介绍了一下FluentValidation验证组件.那里只是概述了一下,并没有对其使用和强大功能做深入研究 ...

  4. canvas学习笔记

    html5的新标签:canvas; 作用:标签定义图形,比如图表和其他图像:标签只是图形容器,您必须使用脚本来绘制图形.默认大小:宽300px,高150px; 背景知识:概念最初由苹果公司提出的,用于 ...

  5. Java内存模型深度解析:重排序 --转

    原文地址:http://www.codeceo.com/article/java-memeory-2.html 数据依赖性 如果两个操作访问同一个变量,且这两个操作中有一个为写操作,此时这两个操作之间 ...

  6. 搞懂$.each和$(selector).each

    $.each:该方法用于遍历任何集合,包括数组和对象 $(selector).each:该方法用于遍历Jquery对象 语法:$.each(obj,callback,args) ①遍历数组 var a ...

  7. 用JPUSH极光推送实现服务端向安装了APP应用的手机推送消息(C#服务端接口)

    这次公司要我们做一个功能,就是当用户成功注册以后,他登录以后要收到消息,当然这个消息是安装了我们的手机APP应用的手机咯. 极光推送的网站的网址是:https://www.jpush.cn/ 极光推送 ...

  8. 故障恢复和恢复模式(Crash Recovery & Recovery Models)

    数据库的恢复模型是否影响故障恢复,在简单恢复模式里,你是否会丢失事务?在今天的文章里我想谈下这点,详细讨论下. 恢复模式(Recovery Models) 对于这个问题的最简单的答案是不会:恢复模型不 ...

  9. 多个提高C#编程能力的建议

    1.总是用属性 (Property) 来代替可访问的数据成员 2.在 readonly 和 const 之间,优先使用 readonly 3.在 as 和 强制类型转换之间,优先使用 as 操作符 4 ...

  10. ASP.NET MVC 解析模板生成静态页一(RazorEngine)

    简述 Razor是ASP.NET MVC 3中新加入的技术,以作为ASPX引擎的一个新的替代项.在早期的MVC版本中默认使用的是ASPX模板引擎,Razor在语法上的确不错,用起来非常方便,简洁的语法 ...