ios - 类别中 self 和[ self 类]之间的区别?

标签 ios objective-c-category

看看我代码中的注释,这两种方法有什么区别?

为什么我不能使用:

[self userDefaultsRead:key];

虽然我可以这样使用类方法:

[self persistenceKey:key];

这是我的代码:enter image description here

最佳答案

self 是对象的实例。您不能以这种方式调用任何类方法(用 + 声明的方法)。

[self class] 返回class对象,才是调用方法的正确方式。这是正确的代码,这就是它起作用的原因。

我建议您回到基础并从头到尾阅读本书。任何你不明白的,研究它直到你明白:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/ProgrammingWithObjectiveC.pdf

关于ios - 类别中 self 和[ self 类]之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25376303/

相关文章:

ios - 字符串格式就像循环中的表 swift4

ios - Swift 中的 Objective-C 协议(protocol)

ios - 使用 UILongPressGestureRecognizer 选择时在 UITableviewCell 周围绘制边框

objective-c - 添加一个大的静态库作为 cocoapod 或直接将所有目标文件添加到生成的二进制文件中,因为 -ObjC 链接器标志

ios - 类别冲突 : instance method in category from conflicts with same method from another category

ios - 尝试调用请求的方法 validateAssets 时出错。 (1272)

ios - Facebook iOS SDK 3.2.1 - [NSError fberrorShouldNotifyUser] : unrecognized selector sent to instance

ios - 关于Object-C中的类别: Override existed message

ios - 何时使用 Categories Objective-C?

ios - 关闭双模式呈现 View Controller 的父 View Controller