IOS开发基础知识--碎片28

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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)#defineweakify_self autoreleasepool{} __weak __typeof__(self) weakSelf = self;#else#defineweakify_self autoreleasepool{} __block __typeof__(self) blockSelf = self;#endif#endif#ifndef strongify_self#if__has_feature(objc_arc)#definestrongify_self try{} @finally{} __typeof__(weakSelf) self = weakSelf;#else#definestrongify_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)#defineweakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;#else#defineweakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;#endif#endif#ifndef strongify#if__has_feature(objc_arc)#definestrongify(object) try{} @finally{} __typeof__(object) strong##_##object = weak##_##object;#else#definestrongify(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_weakself.outputLabel.text =name;

}];

2:objc runtime 动态增加属性

说明:给类扩展增加了一个新属性。通常下类扩展只允许添加方法。必须要先引入 objc/runtime.h,主要是下面两个方法:

赋值:

OBJC_EXPORTvoidobjc_setAssociatedObject(idobject,constvoid*key, id value, objc_AssociationPolicy policy)

获得值:

OBJC_EXPORT id objc_getAssociatedObject(idobject,constvoid*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)staticcharflashColorKey;- (void) setFlashColor:(UIColor *) flashColor{

objc_setAssociatedObject(self,&flashColorKey, flashColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}- (UIColor *) getFlashColor{returnobjc_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];//到毫秒数,则再*1000longlongcurTime=[[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获得缓存大小,并对它进行清除

floattmpSize =[[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 就会有缓存

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航