1. NSAssert()NSCAssert()的使用

NSAssert()用于 OC 语法的断言

NSCAssert()用于 C语言语法的断言

2. Swift3.0使用NSNotification.name

let kOpenXcodePathNotification = "kOpenXcodePathNotification"
NotificationCenter.default.post(name: NSNotification.name, object: url)

上面的代码错误 需要用NSNotification.name进行初始化字符串

let kOpenXcodePathNotification = NSNotification.Name("kOpenXcodePathNotification")

3. 自定义 NSTableView的 Cell

mac开发中使用自定义NSTableCellView

4 . 设置 NSWindow 不允许用户改变大小

设置 ReSize 属性为 NO

5. Unable to find a pod with name, author, summary, or description matching

删除缓存:

rm ~/Library/Caches/CocoaPods/search_index.json

6.一个判断的小坑

if ([PQAccountManager shareManager].isSignIn) {
     // Do One
} else {
     // Do Other
}
if (![PQAccountManager sharedManager].isSignIn) { 
    // Do Other
    return;
}
// Do One

按我们平常的理解上述两个判断是一样的,但是这次我却发现了有问题,他们执行的不一样.

PS:   这块的打印是没有问题的,正常操作后,都是在 0  和 1 切换的
NSLog(@"SignIn === %d,",[PQAccountManager sharedManager].isSignIn);

因此觉的很奇怪,为什么是这样呢?改好了但是却一下子真不懂啊,于是我自己写了一个 demo 测试,发现在那块类似的判断是一样的,所以其中的判断是肯定没问题的,还是我们项目中有问题的。

后来才发现原来是我们项目中有一个:

#import <Foundation/Foundation.h>

/*!
 * 生成单例对象的分类
 */
@interface NSObject (Manager)
/*!
 * 生成单例对象
 * @return 基于 NSObject类的单例对象
 */
+ (instancetype)shareManager;

@end

然后之前管理类中 单例确是: sharedManager,差了一个字母的,所以这种坑,一定要注意细节:

  • 注意细节,字母,单词的准确性
  • 注意私有公用方法添加类似
    - (void)pq_ sharedManager; 
    
    作为区别,同时也提醒了自己,一些 hook 类的方法,能不用就尽量不用,哈哈哈。

7 再次想私有成员变量

今天突然想起,为什么有属性的时候,为什么还要再直接用成员变量呢?它有什么方便之处呢? 首先明确的是 ** 类内使用成员变量{}, 类外使用属性@property,** 所以,此处我说的基本是 .m 文件中使用的成员变量。

@implementation ViewController {
    NSString *_testName;
    NSString *tempStr;
    BOOL isStop;
}

为什么用它呢?

感觉个人平常很少用成员变量,当然除了在 initdeallocgettersetter中 除外咯,其他地方例如临时生成一个tempStr 或者临时的判断值 isStop, 此时是否需要用它呢?

想了想,为了代码的看起来的规范性,我是不愿这样写的。
但是细细想来,一些临时的值确实没必要经过 setter 和 getter 方法,所以想着还是直接用 成员变量的。

PS: 在 Block 中对于成员变量一定要 使用 self-> _testName, 否则直接使用 _testName, 就算添加了 weakSelf/strongSelf 还是会有循环引用的。

8 this class is not key value coding-compliant for the key 错误

对于这种 Bug ,最常见的是我们用 stroyboard 时,某个设置IBAction和IBOutlet时有多余或错误的连线。但是我此处不是的哦,而且这个问题在 stackoverflow 处 已经讨论很多了,而我此处的场景是使用 谓词 时遇到的。

NSPredicate *tempPredicate = [NSPredicate predicateWithFormat:@"cateId == %d", key];
NSArray *filteredTempArray = [array filteredArrayUsingPredicate:tempPredicate];

其实也是很简单,就是一个字母写错了,相当于 key 值换了, 改成正确的值就好了。
cateId === 》 catId (catId 才是之前设置的))。

9 用了 UIImageRenderingModeAlwaysOriginal , 图片颜色倒是变化啦

这个问题是,我们项目中最近在 改变 UITabBarItem 的图片时 使用了获取网络图片,然后对于 selectedImage 必然的做 UIImageRenderingModeAlwaysOriginal 处理,结果却发现那个 颜色变了。。


先再次熟悉下: UIImageRenderingModeAlwaysOriginal 这个属性值。

typedef NS_ENUM(NSInteger, UIImageRenderingMode) {
    UIImageRenderingModeAutomatic,          // Use the default rendering mode for the context where the image is used
    //根据图片的使用环境和所处的绘图上下文自动调整渲染模式
    UIImageRenderingModeAlwaysOriginal,     // Always draw the original image, without treating it as a template
    // 绘制图片原始状态,不使用Tint Color
    UIImageRenderingModeAlwaysTemplate,     // Always draw the image as a template image, ignoring its color information 
    // 根据Tint Color绘制图片,忽略图片的颜色信息
} NS_ENUM_AVAILABLE_IOS(7_0);

网上超经典的图


UIImage *selectImage = [
                        [[YYWebImageManager sharedManager].cache getImageForKey:@"https://example.com/test.png"]
                        imageWithRenderingMode:UIImageRenderingModeAutomatic
                        ];
tabBarItem.selectedImage = selectImage;
UIImage *selectImage = [
                        [[YYWebImageManager sharedManager].cache getImageForKey:@"https://example.com/test.png"]
                        imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
                        ];
tabBarItem.selectedImage = selectImage;

后来发现其实我没错,只是恰好后台配置的是蓝色:

图例

这个错很凑巧,因为 刚好后台 返回的图片也是蓝色 和 灰色,然后就阴差阳错的错了,毕竟看起来是正常的。毕竟 UITabBarItem 默认选中的颜色是 蓝色 和灰色的