Some Tips

作者: weithl | 来源:发表于2016-04-14 11:11 被阅读46次

    1、访问私有属性

    originalURL 是 imageMessage.m 中的属性
    
    /// 通过 valueForKey 或 valueForKeyPath 访问
    self.originalPath = [imageMessage valueForKeyPath:@"thumbnailURL"];
    
    /// 通过 runtime 访问
    Ivar ivar = class_getInstanceVariable([ImageMessage class], "_thumbnailURL");
    id content = object_getIvar(imageMessage, ivar);
    

    2、访问 bundle 中的 json 文件

    NSString *path = [[NSBundle mainBundle] pathForResource:@"test.json" ofType:nil];
    NSData *jsonData = [NSData dataWithContentsOfFile:path];
    NSArray *array = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
    

    3、隐藏导航栏返回标题

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    

    4、weak self

    __weak typeof(self) weakSelf = self;
    

    5、cell 内容超出边界

    cell.clipsToBounds = YES;
    

    6、string to int

    - (int)convertToInt:(NSString*)strtemp
    {
        NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
        NSData* da = [strtemp dataUsingEncoding:enc];
        return [da length];
    }
    

    7、push viewController 时,隐藏 tabbar

    [viewController setHidesBottomBarWhenPushed:YES];
    

    8、在 view 的某些子视图禁用 gesture

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        if ([touch.view isDescendantOfView:self.tableView]) {
            return NO;
        }
        return YES;
    }
    

    9、让 tableview 的 section 滚动

    - (void) scrollViewDidScroll:(UIScrollView *)scrollView {
        CGFloat sectionHeaderHeight = 30;
    
        BOOL isUpSectionScrollToNavigation = scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0;
        BOOL isDownSectionScrollOutNavigation = scrollView.contentOffset.y >= sectionHeaderHeight;
    
        if (isUpSectionScrollToNavigation) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        }else if (isDownSectionScrollOutNavigation){
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
    

    10、在 willDisplayCell:forRowAtIndexPath 设置 字体和颜色

    11、从像素px转换为ios的点阵pt

    + (CGFloat)convertPixlToPoint:(CGFloat)px{
        CGFloat standardPt = px/2.0;
        if (iphone5) {
            standardPt = (iphone5W/iphone6W)*standardPt;
        }else if(iphone6){
            
        }else if(iphone6plus){
            standardPt = (iphone6plusW/iphone6W)*standardPt;
        }else;
        return standardPt;
    }
    

    12、指定集合中的数据类型

    NSArray<NSString *> *strings = @[@"ha0", @"ha1"];
    NSDictionary<NSString *, NSNumber *> *mapping = @{@"a": @1, @"b": @2};
    

    13、Xcode 插件失效

    find ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins -name Info.plist -maxdepth 3 | xargs -I{} defaults write {} DVTPlugInCompatibilityUUIDs -array-add `defaults read /Applications/Xcode.app/Contents/Info.plist DVTPlugInCompatibilityUUID`
    

    14、当前语言

    #define CURRENTLANGUAGE         ([[NSLocale preferredLanguages] objectAtIndex:0])
    

    15、tableview 滑动到顶部

    [tableview scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
    

    16、image 合并

    UIGraphicsBeginImageContext(size);
    [image1 drawInRect:CGRectMake(0, 0, image1.size.width, size.height)];
    [image2 drawInRect:CGRectMake(image1.size.width, 0, image2.size.width, size.height)];
    UIImage *togetherImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    17、系统自带滑动返回

    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    

    18、检测 string 中的链接

    NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
    NSArray* matches = [detector matchesInString:attributedString.string options:0 range:NSMakeRange(0, [attributedString.string length])];
    

    19、用 NSCache 作缓存

    @property (strong, nonatomic) NSCache *photosCache; [self.photosCache objectForKey:photoName];
    [self.photosCache setObject:thumbImage forKey:photoName];
    

    20、array to string

    NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];
    

    21、使用 reveal 的配置

    command alias reveal_load expr (Class)NSClassFromString(@"IBARevealLoader") == nil ? (void*)dlopen((char*)[(NSString*)[(NSBundle*)[NSBundle mainBundle] pathForResource:@"libReveal" ofType:@"dylib"] cStringUsingEncoding:0x4], 0x2) : ((void*)0)
    

    22、关于图片处理

    苹果对 JPEG 有硬编码和硬解码,保存成 JPEG 会大大缩减编码解码时间,也能减小文件体积。如果图片不包含透明像素时,UIImageJPEGRepresentation(0.9) 是最佳的图片保存方式,其次是 UIImagePNGRepresentation()。

    23、Don’t call setNeedsDisplay unless the content of the view has really changed.
    By overriding layoutSubviews, you can make your application much smoother during rotation and scrolling events

    24、一些 sql

    SELECT AGE FROM COMPANY WHERE EXISTS (SELECT AGE FROM COMPANY WHERE SALARY > 65000);
    select coalesce(comm,0) from emp
    select ename, job, deptno from emp where deptno in (10,20) and (ename like '%I%' or job like '%ER')
    select ename, job, sal from emp where deptno = 10 order by 3 desc //3与sal对应
    select e.ename , d.loc from emp e inner join dept d on (e.deptno = d.deptno) where e.deptno = 10
    select e.ename, e.empno, e.job, e.sal, e.deptno from emp e join v2 on(e.ename = v2.ename and e.job = v2.job and e.sal = v2.sal)
    select deptno from dept except select deptno from emp //求差集
    select d.* from dept d left outer join emp e on (d.deptno = e.deptno) where e.deptno is null //outer可选
    update emp set sal = sal * 1.20 where empno in (select empno from emp_bonus)
    //_匹配单个字符
    
    (范围内的数据)SELECT *FROM movies WHERE name BETWEEN 'A' AND 'J';
    (与条件选择)SELECT * FROM movies WHERE year BETWEEN 1990 AND 2000 AND genre = 'comedy';
    (或条件选择)SELECT * FROM movies WHERE genre = 'comedy' OR year < 1980;
    (排序)SELECT * FROM movies ORDER BY imdb_rating DESC;
    (选取前几个)SELECT * FROM movies ORDER BY imdb_rating ASC LIMIT 3;
    
    (计数)SELECT COUNT(*) FROM fake_apps;
    (条件计数)SELECT COUNT(*) FROM fake_apps WHERE price = 0;
    (分组计数)SELECT price ,COUNT(*) FROM fake_apps GROUP BY price;
    (求和)SELECT SUM(downloads) FROM fake_apps;
    (最大值)SELECT MAX(downloads) FROM fake_apps;
    (最小值)SELECT MIN(downloads) FROM fake_apps;
    (平均值)SELECT AVG(downloads) FROM fake_apps ;
    (控制小数点)SELECT price , ROUND(AVG(downloads),2) FROM fake_apps GROUP BY price;
    

    24、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: 毫秒  
    Z:GMT  

    相关文章

      网友评论

          本文标题:Some Tips

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