OC代碼小知識

  1. 可以批量修改光標(biāo)所在位置的變量 control + command + e

  1. 界面左側(cè)的項目導(dǎo)航欄中顯示當(dāng)前文件command + shift + j

  1. UITextField系統(tǒng)自帶邊框
    //設(shè)置邊框樣式  
    //UITextBorderStyleRoundedRect-圓角矩形,背景是白色,不再是透明的  
    //UITextBorderStyleLine-矩形,黑色邊框,透明背景  
    //UITextBorderStyleBezel-和上面類似,但是是灰色的邊框,背景透明  
    textFiled.borderStyle=UITextBorderStyleRoundedRect; 

  1. UITextView設(shè)置placeholder
KVC鍵值編碼,對UITextView的私有屬性進(jìn)行修改(系統(tǒng)版本8.3以上)
UILabel *placeholderLabel = [[UILabel alloc]init];
placeholderLabel.text = @"請輸入內(nèi)容";
placeholderLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
[textView addSubview:placeholderLabel];
[textView setValue:placeholderLabel forKeyPath:@"placeholderLabel"];

  1. 修改UISearBar內(nèi)部背景,placeholder顏色
KVC鍵值編碼
UITextField * searchField = [_searchBar valueForKey:@"_searchField"];
textField.backgroundColor = [UIColor redColor];

[searchField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
[searchField setValue:[UIFont boldSystemFontOfSize:12]forKeyPath:@"_placeholderLabel.font"];

6.是否包含字符串(不區(qū)分大小寫)
localizedCaseInsensitiveContainsString


  1. UIView設(shè)置部分圓角
CGRect rect = view.bounds;
CGSize radio = CGSizeMake(30, 30);//圓角尺寸
UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;//這只圓角位置
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio];
CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//創(chuàng)建shapelayer
masklayer.frame = view.bounds;
masklayer.path = path.CGPath;//設(shè)置路徑
view.layer.mask = masklayer;

  1. dispatch_semaphore信號量
//創(chuàng)建信號量,參數(shù):信號量的初值,如果小于0則會返回NULL
dispatch_semaphore_create(信號量值)
//等待降低信號量
dispatch_semaphore_wait(信號量,等待時間)
//提高信號量
dispatch_semaphore_signal(信號量)

dispatch_queue_t workConcurrentQueue = dispatch_queue_create("cccccccc", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t serialQueue = dispatch_queue_create("sssssssss",DISPATCH_QUEUE_SERIAL);
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
    for (NSInteger i = 0; i < 10; i++) {
        dispatch_async(serialQueue, ^{
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            
            dispatch_async(workConcurrentQueue, ^{
                
                NSLog(@"thread-info:%@開始執(zhí)行任務(wù)%d",[NSThread currentThread],(int)i);
                sleep(1);
                NSLog(@"thread-info:%@結(jié)束執(zhí)行任務(wù)%d",[NSThread currentThread],(int)i);
                
                dispatch_semaphore_signal(semaphore);
            });
            
        });
    }
    NSLog(@"主線程...!");

  1. objc_property_t反射
//objc_property_t:表示類對象中的全局屬性,即用@property定義的屬性
//動態(tài)獲取一個自定義類對象中的所有屬性
- (NSDictionary *)allProperties
{
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for (i = 0; i<outCount; i++)
    {
        objc_property_t property = properties[i];
        const char *char_f =property_getName(property);
        NSString *propertyName = [NSString stringWithUTF8String:char_f];
        id propertyValue = [self valueForKey:(NSString *)propertyName];
        if (propertyValue)
            [props setObject:propertyValue forKey:propertyName];
    }
    free(properties);
    return props;
}

  1. runtime之Ivar
//Ivar:表示類對象中的所有定義的全局變量
//獲取類的成員變量名
+ (NSArray *)getVariableNamesByObject:(id)object
{
    unsigned int numIvars = 0;
    //獲取類的所有成員變量
    Ivar * ivars = class_copyIvarList([object class], &numIvars);
    //定義一個數(shù)組來接收獲取的屬性名
    NSMutableArray *nameArray = [[NSMutableArray alloc] initWithCapacity:numIvars];
    for(int i = 0; i < numIvars; i++) {
        //得到單個的成員變量
        Ivar thisIvar = ivars[i];
        //得到這個成員變量的類型
        const char *type = ivar_getTypeEncoding(thisIvar);
        NSString *stringType =  [NSString stringWithCString:type encoding:NSUTF8StringEncoding];
        //此處判斷非object-c類型時跳過
        if (![stringType hasPrefix:@"@"]) {
            continue;
        }
        //得到成員變量名
        NSString *variableName = [NSString stringWithUTF8String:ivar_getName(thisIvar)];
        [nameArray addObject:variableName];
        
        //這個函數(shù)可以得到成員變量的值
        //object_getIvar(object, thisIvar)
        
    }
    free(ivars);
    return nameArray;
}

歡迎互相學(xué)習(xí)Github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,429評論 4 61
  • 靜默的畫面里,藍(lán)天白云,海風(fēng)交錯,歲月靜好。沒有聲音的愛情,和從未開口卻說出了愛,成就了這年夏天最美最寧靜的海洋 ...
    陳以壹閱讀 2,437評論 3 8
  • 無論你是想減脂還是增肌或者二者兼有,力量訓(xùn)練可以作為核心項目。因 此受到許多健身愛好者的青睞,都會在日常健身訓(xùn)練表...
    健身猶未雪兒閱讀 1,623評論 0 1
  • Nginx 是什么 Nginx 是一款輕量級的 Web服務(wù)器、也是一款反向代理服務(wù)器 Nginx 能干啥 1.可直...
    秋葵2022閱讀 330評論 0 0
  • 小小下班回到家,突然不想做飯了,于是決定去樓下新開的羊湯館喝羊肉湯。 羊湯館里人真多,門口還有幾個排隊的。算了,不...
    子徐閱讀 738評論 0 1

友情鏈接更多精彩內(nèi)容