美文网首页
iOS系统API笔记

iOS系统API笔记

作者: 佐伊Joey | 来源:发表于2016-03-30 17:22 被阅读445次

    class_isMetaClass的使用场景

    NSMutableArray *arr = [NSMutableArray new];
    
    [arr addObject:[NSObject class]];
    [arr addObject:[NSValue class]];
    [arr addObject:[NSNumber class]];
    [arr addObject:[NSPredicate class]];
    [arr addObject:@"not a class object"];
    
    for (int i; i<[arr count]; i++) {
        id obj = [arr objectAtIndex:i];
        
        if(class_isMetaClass(object_getClass(obj)))
        {
            //do sth
            NSLog(@"Class: %@", obj);
        }
        else
        {
            NSLog(@"Instance: %@", obj);
        }
    }
    

    过滤字符串前面和后面的空白和换行

    //常用于聊天信息发送时字符串处理
    [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
    

    利用arc4random_uniform()产生随机数

    OC 中有个arc4random()函数用来生成随机数且不需要种子,但是这个函数生成的随机数范围比较大,需要用取模的算法对随机值进行限制,有点麻烦。其实Objective-C有个更方便的随机数函数arc4random_uniform(x),可以用来产生0~(x-1)范围内的随机数,不需要再进行取模运算。如果要生成1~x的随机数,可以这么写:arc4random_uniform(x)+1


    屏蔽编译警告

    对于关闭某个警告,如果需要全局关闭的话,直接在Other C Flags里写 -Wno-...
    就行了,比如 -Wextra -Wno-sign-compare
    就是一个常见的组合。如果相对某几个文件开启或禁用警告,在Build Phases的Compile Source相应的文件中加入对应的编译标识即可。如果只是想在某几行关闭某个警告的话,可以通过临时改变诊断编译标记来抑制指定类型的警告,具体如下:
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [transaction.target performSelector:transaction.selector];
    #pragma clang diagnostic pop


    多线程group用法

    //1.创建队列组
    dispatch_group_t group = dispatch_group_create();
    //2.创建队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //3.多次使用队列组的方法执行任务, 只有异步方法
    //3.1.执行3次循环
    dispatch_group_async(group, queue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            NSLog(@"group-01 - %@", [NSThread currentThread]);
        }
    });
    //3.2.主队列执行8次循环
    dispatch_group_async(group, dispatch_get_main_queue(), ^{
        for (NSInteger i = 0; i < 8; i++) {
            NSLog(@"group-02 - %@", [NSThread currentThread]);
        }
    });
    //3.3.执行5次循环
    dispatch_group_async(group, queue, ^{
        for (NSInteger i = 0; i < 5; i++) {
            NSLog(@"group-03 - %@", [NSThread currentThread]);
        }
    });
    //4.都完成后会自动通知
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"完成 - %@", [NSThread currentThread]);
    });
    

    创建屏幕截图系统API

    @interface UIScreen (UISnapshotting)
    // Please see snapshotViewAfterScreenUpdates: in UIView.h for some important details on the behavior of this method when called from layoutSubviews.
    - (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0);
    @end
    

    iOS-NSDateFormatter 格式说明

    转载

    格式化参数如下:
    G: 公元,例如AD公元
    yy: 年的后2位
    yyyy: 完整年
    MM: 月,显示为1-12
    MMM: 月,显示为月份简写,如 Jan
    MMMM: 月,显示为英文月份全称,如 Janualy
    dd: 日,2位数表示,如02
    d: 日,1-2位显示,如 2
    EEE: 简写星期几,如Sun
    EEEE: 全写星期几,如Sunday
    aa: 上下午,AM/PM
    H: 时,24小时制,0-23
    K:时,12小时制,0-11
    m: 分,1-2位
    mm: 分,2位
    s: 秒,1-2位
    ss: 秒,2位
    S: 毫秒

    常用日期结构:
    yyyy-MM-dd
    HH:mm:ss.SSS
    yyyy-MM-dd
    HH:mm:ss
    yyyy-MM-dd
    MM dd yyyy


    根据图片填充相应颜色

    原图Mask
    填充颜色后效果
    - (UIImage *)jsq_imageMaskedWithColor:(UIColor *)maskColor
    {
        NSParameterAssert(maskColor != nil);
        
        CGRect imageRect = CGRectMake(0.0f, 0.0f, self.size.width, self.size.height);
        UIImage *newImage = nil;
        
        UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, self.scale);
        {
            CGContextRef context = UIGraphicsGetCurrentContext();
            
            CGContextScaleCTM(context, 1.0f, -1.0f);
            CGContextTranslateCTM(context, 0.0f, -(imageRect.size.height));
            
            CGContextClipToMask(context, imageRect, self.CGImage);
            CGContextSetFillColorWithColor(context, maskColor.CGColor);
            CGContextFillRect(context, imageRect);
            
            newImage = UIGraphicsGetImageFromCurrentImageContext();
        }
        UIGraphicsEndImageContext();
        
        return newImage;
    }
    

    怎么改变uitextfield placeholder的颜色和位置

    //继承uitextfield,重写这个方法
    - (void) drawPlaceholderInRect:(CGRect)rect {
        [[UIColor blueColor] setFill];
        [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
    }
    

    把navigationbar弄成透明的而不是带模糊的效果

    [self.navigationBar setBackgroundImage:[UIImage new]
                             forBarMetrics:UIBarMetricsDefault];
    self.navigationBar.shadowImage = [UIImage new];
    self.navigationBar.translucent = YES;
    

    拦截网页图片 并修改图片大小

    [aWebView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth=320;" //缩放系数
     "var images = document.images;"
     "alert(images.length);"
     "for(i=0;i <images.length;i++){"
     "myimg = images[i];"
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.style.width = maxwidth;"
     "myimg.style.height = myimg.height * (maxwidth/oldwidth);"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [aWebView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    

    最简单的获取网络get的数据

    NSError *error = nil;
    NSString *environmentResult = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://211.144.118.118/v1.0.0/house/job/get_url"] encoding:NSUTF8StringEncoding error:&error];
    

    获取某个类的成员变量、属性、方法

    unsignedint numIvars; //成员变量个数
    Ivar *vars = class_copyIvarList(NSClassFromString(@"BMKLocationService"), &numIvars);
    
    NSString *key=nil;
    for(int i = 0; i < numIvars; i++) {
        
        Ivar thisIvar = vars[i];
        key = [NSStringstringWithUTF8String:ivar_getName(thisIvar)];  //获取成员变量的名字
        NSLog(@"variable name :%@", key);
        key = [NSStringstringWithUTF8String:ivar_getTypeEncoding(thisIvar)]; //获取成员变量的数据类型
        NSLog(@"variable type :%@", key);
    }
    free(vars);
    
    
    Method*meth =class_copyMethodList(NSClassFromString(@"UIView"), &numIvars);
    
    for(inti =0; i < numIvars; i++) {
        MethodthisIvar = meth[i];
        
        SELsel =method_getName(thisIvar);
        constchar*name =sel_getName(sel);
        
        NSLog(@"zp method :%s", name);
    }
    free(meth);
    
    returnYES;
    

    过滤掉除了数字之外的字符

    NSString* simplePhoneNumber =
    [[phoneNumber componentsSeparatedByCharactersInSet:
      [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
     componentsJoinedByString:@""];
    

    OC、C与C++ 字符串转换

    //char * /const char * 转NSString
    NSString * strPath = [NSString stringWithUTF8String:filename];
    
    //NSString转char * /const char *
    const char * filePathChar = [filePath UTF8String];
    
    //转化char 到NSString
    char myChar ='a';
    NSString* string =[NSString stringWithFormat:@"%c", myChar];
    
    //提取NSString的某个字段到char
    - (unichar)characterAtIndex:(NSUInteger)index;
    
    // c++ 和 oc的本身是不能直接对接的。要通过c的api做连接的。
    // string 转 NSString
    string str = [aNSString UTF8String];
    
    //NSString 转 string
    string str("testStr");
    NSString * aString = [NSString stringWithUTF8String:str.c_str()];
    

    方法 可变参数

    - (id)initWithTitle:(NSString*)title delegate:(id<IBActionSheetDelegate>)delegate cancelButtonTitle:(NSString*)cancelTitle destructiveButtonTitle:(NSString*)destructiveTitle otherButtonTitles:(NSString*)otherTitles, ... {
        
        self = [self init];
        self.delegate= delegate;
        self.backgroundColor= [UIColorclearColor];
        
        NSMutableArray*titles = [[NSMutableArrayalloc]init];
        
        if(otherTitles) {
            va_listargs;
            va_start(args, otherTitles);
            for(NSString*arg = otherTitles; arg !=nil; arg =va_arg(args,NSString* ))
            {
                [titlesaddObject:arg];
            }
            va_end(args);
        }
    }
    

    强制继承重写方法

    - (BOOL)isFinished
    {
        [self doesNotRecognizeSelector:_cmd];
        return NO;
        
    }
    

    (未完待续)

    相关文章

      网友评论

          本文标题:iOS系统API笔记

          本文链接:https://www.haomeiwen.com/subject/rciklttx.html