iOS Tricks

2018-07-31  本文已影响18人  cc_Jumper

标记:
1-E-1 : 对第1的补充1(1-Extend-1)

1.自定义日志打印接口,方便在console filter中过滤输出,只关心自己的打印,在开发新的业务功能的时候很好用

// 自定义日志TAG
// 可以在XCode的Console中Filter里面输入kTagMyLog来过滤只显示 MyLog方法打印的日志
// 可以在XCode的Console中Filter里面输入kTagMyBusiness来过滤这次需求相关的日志,如SYGB(首页改版),tag可以依据需求改动
#define kTagMyLog @"MyLog"
#define kTagMyBusiness @"SYGB"
#define _Log(tag, s) do { \
NSLog([NSString stringWithFormat:@"[%@][%@]%sLine:%d: %@",\
tag,\
[[NSThread currentThread] isMainThread] ? @"Main" : @"NoMain",\
__PRETTY_FUNCTION__,\
__LINE__,\
s]); \
} while (0)
#define MyLog(format, ...) _Log(kTagMyLog, (format, ##__VA_ARGS__))
#define MyBusLog(format, ...) _Log(kTagMyBusiness, (format, ##__VA_ARGS__))

1-E-1 宏定义中的do while防御式编程

注意:使用do while可防御一部分代码书写方式,比如有些同学在使用if else语句的时候,在if分支因为只调用一句宏,故省略掉外括号的情况

// 虽然实际情况下不会这样子输出`及格了`,这里只是举例
#define printGoodGrade1 \
NSLog(@"及"); \
NSLog(@"格"); \
NSLog(@"了"); \

#define printGoodGrade2 \
{ \
NSLog(@"及"); \
NSLog(@"格"); \
NSLog(@"了"); \
}

#define printGoodGrade3 \
do { \
NSLog(@"及"); \
NSLog(@"格"); \
NSLog(@"了"); \
} while(0)

- (void)test {
    int grade1 = 59;
    int grade2 = 61;
    
    // 🌫case1 logic error 也会输出 格,了两个字
    if (grade1 > 60)
        printGoodGrade1;

    // 🌫case2 compile error
    if (grade2 > 60)
        printGoodGrade2;
    /*
     宏展开-->
     {
     NSLog(@"及");
     NSLog(@"格");
     NSLog(@"了");
     };  // 多一个分号,会导致编译报错. Expected expression
     */
    else {
        NSLog(@"not qulified");
    }
    // 🌫case3 run proper, but attention this macro can't end with character ";", so it's not best macro define
    if (grade2 > 60)
        printGoodGrade2
    else {
        NSLog(@"not qulified");
    }
    // 🌈case4 run proper, user use 'if else' case with not bracket wrapper also guard right, so it's best macro define
    if (grade2 > 60)
        printGoodGrade3;
    else {
        NSLog(@"not qulified");
    }
}

🌈结论:在定义宏的时候,如果是有多条语句,请使用do {} while(0)包裹 🌈

2.数据测试网站

3.Instruments使用技巧

如果使用 Instruments 查看调用堆栈时可能看到的都是地址而不是函数名,这样就不太好定位问题了。这时候你可以把相关项目的 Build Settings - Debug Information Format 的 Debug 和 Release 都设置为 DWARF with dSYM File,这样就能将对应的堆栈信息符号化显示了

4.Xcode常用路径

  1. 在Xcode > Preferences > Components下载的模拟器路径如下

/Library/Developer/CoreSimulator/Profiles/Runtimes

可以直接从该目录提取下载好的模拟器传给他人使用(同样放在该目录下),而不用每次下载

  1. Xcode自定义主题路径

~/Library/Developer/Xcode/UserData/FontAndColorThemes/

如果没有该目录,可以在UserData下手动创建FontAndColorThemes文件夹,然后将主题文件(userDefines.xccolortheme)放入其中,完全 退出Xcode后,启动就可以被识别到,然后可以在 Xcode > Preferences > Fonts & Colors里面设置刚才导入的主题

强烈推荐的主题:
https://draculatheme.com/xcode/

  1. Archives路径

/Users/pengchangcheng/Library/Developer/Xcode/Archives

  1. DerivedData路径

/Users/pengchangcheng/Library/Developer/Xcode/DerivedData

上一篇下一篇

猜你喜欢

热点阅读