OC代码小知识

  1. 可以批量修改光标所在位置的变量 control + command + e

  1. 界面左侧的项目导航栏中显示当前文件command + shift + j

  1. UITextField系统自带边框
    //设置边框样式  
    //UITextBorderStyleRoundedRect-圆角矩形,背景是白色,不再是透明的  
    //UITextBorderStyleLine-矩形,黑色边框,透明背景  
    //UITextBorderStyleBezel-和上面类似,但是是灰色的边框,背景透明  
    textFiled.borderStyle=UITextBorderStyleRoundedRect; 

  1. UITextView设置placeholder
KVC键值编码,对UITextView的私有属性进行修改(系统版本8.3以上)
UILabel *placeholderLabel = [[UILabel alloc]init];
placeholderLabel.text = @"请输入内容";
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内部背景,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.是否包含字符串(不区分大小写)
localizedCaseInsensitiveContainsString


  1. UIView设置部分圆角
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];//创建shapelayer
masklayer.frame = view.bounds;
masklayer.path = path.CGPath;//设置路径
view.layer.mask = masklayer;

  1. dispatch_semaphore信号量
//创建信号量,参数:信号量的初值,如果小于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:%@开始执行任务%d",[NSThread currentThread],(int)i);
                sleep(1);
                NSLog(@"thread-info:%@结束执行任务%d",[NSThread currentThread],(int)i);
                
                dispatch_semaphore_signal(semaphore);
            });
            
        });
    }
    NSLog(@"主线程...!");

  1. objc_property_t反射
//objc_property_t:表示类对象中的全局属性,即用@property定义的属性
//动态获取一个自定义类对象中的所有属性
- (NSDictionary *)allProperties
{
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for (i = 0; i

  1. runtime之Ivar
//Ivar:表示类对象中的所有定义的全局变量
//获取类的成员变量名
+ (NSArray *)getVariableNamesByObject:(id)object
{
    unsigned int numIvars = 0;
    //获取类的所有成员变量
    Ivar * ivars = class_copyIvarList([object class], &numIvars);
    //定义一个数组来接收获取的属性名
    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];
        
        //这个函数可以得到成员变量的值
        //object_getIvar(object, thisIvar)
        
    }
    free(ivars);
    return nameArray;
}

欢迎互相学习Github

你可能感兴趣的:(OC代码小知识)