iOS零碎知識點<中階版>

iOS零碎知識點<初級版>
iOS零碎知識點<中階版>
iOS零碎知識點<高階版>
iOS零碎知識點<工具篇>

獲取屬性列表

            unsigned int count = 0;
            Ivar *members = class_copyIvarList([obj class], &count);
            for (int i = 0 ; i < count; i++) {
                Ivar var = members[i];
                //獲取變量名稱
                const char *memberName = ivar_getName(var);
                //獲取變量類型
                const char *memberType = ivar_getTypeEncoding(var);
                
                NSLog(@"%s----%s", memberName, memberType);
            }

//重新給屬性賦值
//"_callDelegate" 為屬性名
            Ivar var = class_getInstanceVariable([obj class], "_callDelegate");
            object_setIvar(obj, var, self);


獲取類的所有屬性

/**  
 *  獲取類的所有屬性  
 */  
+(void)showStudentClassProperties  
{  
    Class cls = [Student class];  
    unsigned int count;  
    while (cls!=[NSObject class]) {  
        objc_property_t *properties = class_copyPropertyList(cls, &count);  
        for (int i = 0; i<count; i++) {  
            objc_property_t property = properties[i];  
            NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];  
            NSLog(@"屬性名==%@",propertyName);  
        }  
        if (properties){  
            //要釋放  
           free(properties);  
        }  
    //得到父類的信息  
    cls = class_getSuperclass(cls);  
    }  
} 


獲取類的所有方法

/**  
 *  獲取類的所有方法  
 */  
+(void)showStudentClassMethods  
{  
    unsigned int count;  
    Class cls = [Student class];  
    while (cls!=[NSObject class]){  
        Method *methods = class_copyMethodList(cls, &count);  
        for (int i=0; i < count; i++) {  
            NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding];  
            NSLog(@"方法名:%@ ", methodName);  
        }  
        if (methods) {  
             free(methods);  
        }  
        cls = class_getSuperclass(cls);  
    }  
     
}  


獲取類的所有屬性 做健值反射

/**  
 *  獲取類的所有屬性 做健值反射  
 */  
+(Student *)showStudentClassPropertiesToMapValueWithDic:(NSDictionary *)dic  
{  
    Student *stu = [[Student alloc] init];  
      
    Class cls = [Student class];  
    unsigned int count;  
    while (cls!=[NSObject class]) {  
        objc_property_t *properties = class_copyPropertyList(cls, &count);  
        for (int i = 0; i<count; i++) {  
            objc_property_t property = properties[i];  
            NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];  
            //得到屬性名可以在這里做反射 反饋過來的dic 直接反射成一個model   
            /**  
             *  valueForKey: 是 KVC(NSKeyValueCoding) 的方法,在 KVC 里可以通過 property 同名字符串來獲取對應的值  
             *  這里的dic 的key 與 stu 的屬性名一一對應  
             * (MVC模式設計數據反射時候用到{類定義屬性時候要和服務端反饋過來的字段一樣})  
             */  
            id propertyValue = [dic valueForKey:propertyName];  
            if (propertyValue)  
                [stu setValue:propertyValue forKey:propertyName];//屬性get set賦值  
            // NSLog(@"%@ = %@",propertyName,propertyValue);  
        }  
        if (properties){  
            free(properties);  
        }  
        //得到父類的信息  
        cls = class_getSuperclass(cls);  
    }  
    return stu;  
}  


判別這個屬性是否具備get set方法

// 判別這個屬性是否具備get set方法 (重要血当!以上都要添加這個判別)
NSString *propertySetMethod = [NSString stringWithFormat:@"set%@%@:", [[propertyName substringToIndex:1] capitalizedString]  ,[propertyName substringFromIndex:1]];
SEL selector = NSSelectorFromString(propertySetMethod);
if ([model respondsToSelector:selector])
{
    [model setValue:propertyValue forKey:propertyName];
}


高寬比例計算方法及等比高寬修改計算方法

假設:
高=G,寬=K,比例=B;

比例公式:
B= G / K;

等比修正公式:
如果改變寬度(K),則高度(G)計算公式應為:K * B;
如果改變高度(G)油宜,則寬度(K)計算公式應為:G / B;

代入值進行計算:
Size = 1024; (默認縮放值)
G = 111, K = 370;
B = G / K; (雙精度浮點數)

修改寬度
K = 1024, G = K * B; (四舍五入取整)

修改高度
G = 1024, K = G / B; (四舍五入取整)

書籍寬高計算比例:
- (void)scale {
    NSInteger width = 1280;
    NSInteger height = 720;
    NSInteger sacle = getScale(width, height);
    NSLog(@"%ld :%ld",width / sacle, height / sacle);
}

NSInteger getScale(NSInteger w, NSInteger h) {
    if (w % h) {
        return gcd(h, w % h);
    } else {
        return h;
    }
}


判定是否設置了網絡代理

需要導入框架CFNetwork另伍,然后宪睹,這個方法是MRC的:需要添加-fno-objc-arc的flag,代碼如下:

+ (BOOL)getProxyStatus {
    NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary*)CFNetworkCopySystemProxySettings() autorelease]);
    NSArray *proxies = NSMakeCollectable([(NSArray*)CFNetworkCopyProxiesForURL((CFURLRef)[NSURL URLWithString:@"http://www.google.com"], (CFDictionaryRef)proxySettings) autorelease]);
    NSDictionary *settings = [proxies objectAtIndex:0];
    NSLog(@"host=%@", [settings objectForKey:(NSString*)kCFProxyHostNameKey]);
    NSLog(@"port=%@", [settings objectForKey:(NSString*)kCFProxyPortNumberKey]);
    NSLog(@"type=%@", [settings objectForKey:(NSString*)kCFProxyTypeKey]);
    if ([[settings objectForKey:(NSString *)kCFProxyTypeKey] isEqualToString:@"kCFProxyTypeNone"])
    {
        //沒有設置代理
        return NO;
    } else {
        //設置代理了
        return YES;
    }
}


漢字轉拼音

+ (NSString *)transform:(NSString *)chinese
{    
    //將NSString裝換成NSMutableString
    NSMutableString *pinyin = [chinese mutableCopy];    
    //將漢字轉換為拼音(帶音標)    
    CFStringTransform((__bridgeCFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);    
    NSLog(@"%@", pinyin);    
    //去掉拼音的音標    
    CFStringTransform((__bridgeCFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);    
    NSLog(@"%@", pinyin);    
    //返回最近結果    
    return pinyin;
 }
- (WKWebView *)wk_WebView{
    if (!_wk_WebView) {
        // 禁止選擇CSS
        NSString *css = @"body{-webkit-user-select:none;-webkit-user-drag:none;}";
        
        // CSS選中樣式取消
        NSMutableString *javascript = [NSMutableString string];
        [javascript appendString:@"var style = document.createElement('style');"];
        [javascript appendString:@"style.type = 'text/css';"];
        [javascript appendFormat:@"var cssContent = document.createTextNode('%@');", css];
        [javascript appendString:@"style.appendChild(cssContent);"];
        [javascript appendString:@"document.body.appendChild(style);"];
        
        // javascript注入
        WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *userContentController = [[WKUserContentController alloc] init];
        [userContentController addUserScript:noneSelectScript];
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        config.preferences = [[WKPreferences alloc] init];
        config.userContentController = userContentController;
        [config.userContentController addScriptMessageHandler:self name:ScriptMessageHandlerName];
        //        [_wk_WebView evaluateJavaScript:ScriptMessageHandlerName completionHandler:^(id _Nullable response, NSError * _Nullable error) {
        //            NSLog(@"response: %@ error: %@", response, error);
        //            NSLog(@"call js alert by native");
        //        }];
        _wk_WebView = [[WKWebView alloc] initWithFrame:self.view.bounds
                                         configuration:config];
        _wk_WebView.UIDelegate = self;
        _wk_WebView.navigationDelegate = self;
        _wk_WebView.scrollView.bounces = NO;
        _wk_WebView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, kNavgationHeight, 0);
        //側滑
        _wk_WebView.allowsBackForwardNavigationGestures = YES;
        if (IOS_VERSION_10_OR_LATER) {
            _wk_WebView.scrollView.refreshControl = self.refreshControl;
        }
        // 添加KVO監(jiān)聽 //進度
        [_wk_WebView addObserver:self forKeyPath:ObserveWebKey_Progress options:NSKeyValueObservingOptionNew context:NULL];
    }
    return _wk_WebView;
}

獲取CPU核數

unsigned int countOfCores()
{
  unsigned int ncpu;
  size_t len = sizeof(ncpu);
  sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0);
  
  return ncpu;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末岗照,一起剝皮案震驚了整個濱河市村象,隨后出現(xiàn)的幾起案子笆环,更是在濱河造成了極大的恐慌,老刑警劉巖厚者,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件躁劣,死亡現(xiàn)場離奇詭異,居然都是意外死亡库菲,警方通過查閱死者的電腦和手機账忘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來熙宇,“玉大人鳖擒,你說我怎么就攤上這事√讨梗” “怎么了蒋荚?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵傻盟,是天一觀的道長栗精。 經常有香客問我,道長殉了,這世上最難降的妖魔是什么互躬? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任播赁,我火速辦了婚禮,結果婚禮上吼渡,老公的妹妹穿的比我還像新娘容为。我一直安慰自己,他們只是感情好诞吱,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布舟奠。 她就那樣靜靜地躺著,像睡著了一般房维。 火紅的嫁衣襯著肌膚如雪沼瘫。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天咙俩,我揣著相機與錄音耿戚,去河邊找鬼。 笑死阿趁,一個胖子當著我的面吹牛膜蛔,可吹牛的內容都是我干的。 我是一名探鬼主播脖阵,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼皂股,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了命黔?” 一聲冷哼從身側響起呜呐,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤就斤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后蘑辑,有當地人在樹林里發(fā)現(xiàn)了一具尸體洋机,經...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年洋魂,在試婚紗的時候發(fā)現(xiàn)自己被綠了绷旗。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡副砍,死狀恐怖衔肢,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情址晕,我是刑警寧澤膀懈,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站谨垃,受9級特大地震影響,放射性物質發(fā)生泄漏硼控。R本人自食惡果不足惜刘陶,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望牢撼。 院中可真熱鬧匙隔,春花似錦、人聲如沸熏版。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽撼短。三九已至再膳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間曲横,已是汗流浹背喂柒。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留禾嫉,地道東北人灾杰。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像熙参,于是被迫代替她去往敵國和親艳吠。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內容