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;@endUILabel+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-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];//到毫秒數,則再*1000long long curTime=[[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獲得緩存大小,并對它進行清除
float tmpSize = [[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 就會有緩存
新聞熱點
疑難解答