判斷系統(tǒng)的版本代碼:
//系統(tǒng)版本大于~~
#define SYSTEM_VERSION_GREATER_THAN_TO(v) ([[[UIDevice currentDevice]systemVersion]compare:v options:NSNumericSearch]!=NSOrderedAscending)
//系統(tǒng)版本小于~~
#define SYSTEM_VERSIOM_LESS_THAN(v) ([[[UIDevice currentDevice]systemVersion]compare:v options:NSNumericSearch]==NSOrderedAscending)
//在6.0到6.1版本內(nèi)才會執(zhí)行
if ((SYSTEM_VERSION_GREATER_THAN_TO(@"8.0"))&&(SYSTEM_VERSIOM_LESS_THAN(@"8.1")))
{
//執(zhí)行
}
枚舉
上文中 NSOrderedAscending 是枚舉類型 為-1,相關(guān)如下
typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};
在某些版本中替換掉系統(tǒng)的方法
//要替換系統(tǒng)方法的類 UIImagePickerController
//shouldAutorotate 要替換的方法
Method oldMethod1=class_getInstanceMethod([UIImagePickerController class], @selector(shouldAutorotate));
//新寫的方法
Method newMethod1=class_getInstanceMethod([ImagePickerReplaceMethodsHolder class], @selector(shouldAutorotate));
//執(zhí)行替換
method_setImplementation(oldMethod1, method_getImplementation(newMethod1));
Method oldMethod2=class_getInstanceMethod([UIImagePickerController class], @selector(preferredInterfaceOrientationForPresentation));
Method newMethod2=class_getInstanceMethod([ImagePickerReplaceMethodsHolder class], @selector(preferredInterfaceOrientationForPresentation));
method_setImplementation(oldMethod2, method_getImplementation(newMethod2));
交換兩個(gè)類方法
method_exchangeImplementations(m1, m2);
//交換兩個(gè)方法的實(shí)現(xiàn)時(shí)候使用
替換類中的方法
class_replaceMethod替換類方法
1.當(dāng)類中沒有想替換的原方法的時(shí)候,該方法會調(diào)用class_addMethod來為該類增加一個(gè)新方法
2.當(dāng)需要替換的方法可能不存在的時(shí)候,可以考慮該方法
OC中大寫自動分割代碼
//a 97 大 A65
NSString * string=@"MyNameIsYinYuGuang";
NSRegularExpression * regexp=[NSRegularExpression regularExpressionWithPattern:@"([a-z])([A-Z])" options:0 error:NULL];
NSString * newString=[regexp stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, string.length) withTemplate:@"$1 $2"];
NSLog(@"Changed '%@'->'%@'",string,newString);
我是demo
QQ交流群 298975638
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。