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. MySQL学习笔记十四:优化(1)

    SQL优化 1.查看各种SQL执行的频率 mysql> show status like 'Com_select';--Com_insert,Com_delete,connections(试图连 ...

  2. C算法编程题(四)上三角

    前言 上一篇<C算法编程题(三)画表格> 上几篇说的都是根据要求输出一些字符.图案等,今天就再说一个“上三角”,有点类似于第二篇说的正螺旋,输出的字符少了,但是逻辑稍微复杂了点. 程序描述 ...

  3. (八)WebGIS中栅格图层的设计

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.    前言 我们在上一章里了解到WebGIS中栅格图层的本质—— ...

  4. Compute Resource Consolidation Pattern 计算资源整合模式

    Consolidate multiple tasks or operations into a single computational unit. This pattern can increase ...

  5. jQuery-1.9.1源码分析系列(十五) 动画处理——缓动动画核心Tween

    在jQuery内部函数Animation中调用到了createTweens()来创建缓动动画组,创建完成后的结果为: 可以看到上面的缓动动画组有四个原子动画组成.每一个原子动画的信息都包含在里面了. ...

  6. JAVA keytool 使用详解

      Keytool是一个Java数据证书的管理工具 ,Keytool将密钥(key)和证书(certificates)存在一个称为keystore的文件中在keystore里,包含两种数据: 密钥实体 ...

  7. 使用EncryptByPassPhrase和DecryptByPassPhrase对MS SQLServer某一字段时行加密和解密

    在数据库实现加密与解密的文章,Insus.NET较早前也有写过,可以在本博客中可以搜索得到. 今天使用EncryptByPassPhrase和DecryptByPassPhrase来简单实现. 在数据 ...

  8. Repeater 控件

    Repeater 控件是一个容器控件,可用于从网页的任何可用数据中创建自定义列表.Repeater 控件没有自己内置的呈现功能,这意味着用户必须通过创建模板来提供 Repeater 控件的布局.当网页 ...

  9. javascript系统时间测试题

    如果系统的时间是2016年2月20日,分析下列JavaScript代码,运行后在网页上显示() var now = new Date();var year = now.getFullYear();va ...

  10. 三分钟集成elmah xml 格式日志到mvc站点

    1.通过nuget安装Elmah ELMAH on XML Log 会自动在web.config 文件中添加配置内容,默认不允许远程访问,日志访问路径是 Elmah.axd,不记录500错误 2.修改 ...