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. codeforces B. Ohana Cleans Up

    B. Ohana Cleans Up Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid ...

  2. .Net 转战 Android 4.4 日常笔记(6)--Android Studio DDMS用法

    Android Studio DDMS与Eclipse DDMS大同小异,下面了解DDMS的使用 DDMS(Dalvik Debug Monitor Service )Dalvik调试监控服务 DDM ...

  3. UWP开发之Mvvmlight实践三:简单MVVM实例开发(图文详解付代码)

    在做MVVM各种框架对比之前,我觉得有必要先自己做一个简单的MVVM实现案例比较好,这样就可以看到自己实现的时候有那些不方便的地方.而各种框架又是怎么解决我们这些麻烦的. 案例介绍:用户登录画面,没有 ...

  4. 1Z0-053 争议题目解析520

    1Z0-053 争议题目解析520 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 520.Which of the following are not disabled by de ...

  5. Myeclipse无法开启Servers视图解决办法

    IDE报错如下:  解决办法:1.首先关闭MyEclipse工作空间. 2.然后删除工作空间下的 “/.metadata/.plugins/org.eclipse.core.runtime/.sett ...

  6. 【写给大家看的CSS】定位元素:使用position/display布局

    引言 使用position和display来实现的当鼠标移动到图片上时显示和隐藏文字或者视频的效果. 知识点 1.relative.absolute的使用. 当子元素使用position:absolu ...

  7. mysql数据库去重复

    参考:http://www.cnblogs.com/duanjie/archive/2011/08/13/2136862.html 说到去重复,感觉逻辑很简单.但动手写起来却并不是那么容易.面试的时候 ...

  8. 我的angularjs源码学习之旅1——初识angularjs

    angular诞生有好几年光景了,有Google公司的支持版本更新还是比较快,从一开始就是一个热门技术,但是本人近期才开始接触到.只能感慨自己学习起点有点晚了.只能是加倍努力赶上技术前线. 因为有分析 ...

  9. 数据结构Java实现01----算法概述

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  10. C#获取图片的后缀名

    最近在学习过程中遇到一个问题,就是如何获取图片的格式,对于一张知道全路径的照片,如果其路径包含后缀名的话,要取得后缀名,只需要一行代码即可: var ext = System.IO.Path.GetE ...