objc’s self and super 详解

本文深入探讨了Objective-C中的self和super关键字的作用及工作原理,通过代码示例展示了如何在子类中调用父类方法,并详细解释了self和super在方法调用过程中的区别和实现机制。

在objc中的类实现中经常看到这两个关键字”self”和”super”,以以前oop语言的经验,拿c++为例,self相当于this,super相当于调用父类的方法,这么看起来是很容易理解的。

 

以下面的代码为例:

 

@interface Person:NSObject {
     NSString*  name;
}
- ( void ) setName:(NSString*) yourName;
@end
 
@interface PersonMe:Person {
     NSUInteger age;
}
- ( void ) setAge:(NSUInteger) age;
- ( void ) setName:(NSString*) yourName andAge:(NSUInteger) age;
@end
 
@implementation PersonMe
- ( void ) setName:(NSString*) yourName andAge:(NSUInteger) age {
     [self setAge:age];
     [super setName:yourName];
}
@end
 
int main( int argc, char * argv[]) {
     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]
     PersonMe* me = [[PersonMe alloc] init];
     [me setName:@ "asdf" andAge:18];
     [me release];
     [pool drain];
     return 0;
}

 

上面有简单的两个类,在子类PersonMe中调用了自己类中的setAge和父类中的setName,这些代码看起来很好理解,没什么问题。
然后我在setName:andAge的方法中加入两行:

 



NSLog(@ "self ' class is %@" , [self class ]);
NSLog(@ "super' class is %@" , [super class ]);

 

这样在调用时,会打出来这两个的class,先猜下吧,会打印出什么?
按照以前oop语言的经验,这里应该会输出:

self ' s class is PersonMe
super ' s class is Person

但是编译运行后,可以发现结果是:

self 's class is PersonMe
super ' s class is PersonMe

self的class和预想的一样,怎么super的class也是PersonMe?

真相

self 是类的隐藏的参数,指向当前当前调用方法的类,另一个隐藏参数是_cmd,代表当前类方法的selector。这里只关注这个self。super是个 啥?super并不是隐藏的参数,它只是一个“编译器指示符”,它和self指向的是相同的消息接收者,拿上面的代码为例,不论是用[self setName]还是[super setName],接收“setName”这个消息的接收者都是PersonMe* me这个对象。不同的是,super告诉编译器,当调用setName的方法时,要去调用父类的方法,而不是本类里的。

当使用self调用方法时,会从当前类的方法列表中开始找,如果没有,就从父类中再找;而当使用super时,则从父类的方法列表中开始找。然后调用父类的这个方法。

One more step

这种机制到底底层是如何实现的?其实当调用类方法的时候,编译器会将方法调用转成一个C函数方法调用,apple的objcRuntimeRef上说:

Sending Messages

When it encounters a method invocation, the compiler might generate a call to any of several functions to perform the actual message dispatch, depending on the receiver, the return value, and the arguments. You can use these functions to dynamically invoke methods from your own plain C code, or to use argument forms not permitted by NSObject’s perform… methods. These functions are declared in /usr/include/objc/objc-runtime.h.
■ objc_msgSend sends a message with a simple return value to an instance of a class.
■ objc_msgSend_stret sends a message with a data-structure return value to an instance of
a class.
■ objc_msgSendSuper sends a message with a simple return value to the superclass of an instance of a class.
■ objc_msgSendSuper_stret sends a message with a data-structure return value to the superclass of an instance of a class.

可以看到会转成调用上面4个方法中的一个,由于_stret系列的和没有_stret的那两个类似,先只关注objc_msgSend和objc_msgSendSuper两个方法。

当使用[self setName]调用时,会使用objc_msgSend的函数,先看下objc_msgSend的函数定义:

 



id objc_msgSend(id theReceiver, SEL theSelector, ...)


第一个参数是消息接收者,第二个参数是调用的具体类方法的selector,后面是selector方法的可变参数。我们先不管这个可变参数,以 [self setName:]为例,编译器会替换成调用objc_msgSend的函数调用,其中theReceiver是self,theSelector是 @selector(setName:),这个selector是从当前self的class的方法列表开始找的setName,当找到后把对应的 selector传递过去。

而当使用[super setName]调用时,会使用objc_msgSendSuper函数,看下objc_msgSendSuper的函数定义:

 



id objc_msgSendSuper( struct objc_super *super, SEL op, ...)


第一个参数是个objc_super的结构体,第二个参数还是类似上面的类方法的selector,先看下objc_super这个结构体是什么东西:

 


struct objc_super {
     id receiver;
     Class superClass;
};

 

可以看到这个结构体包含了两个成员,一个是receiver,这个类似上面objc_msgSend的第一个参数receiver,第二个成员是记 录写super这个类的父类是什么,拿上面的代码为例,当编译器遇到PersonMe里setName:andAge方法里的[super setName:]时,开始做这几个事:

1,构建objc_super的结构体,此时这个结构体的第一个成员变量receiver就是PersonMe* me,和self相同。而第二个成员变量superClass就是指类Person,因为PersonMe的超类就是这个Person。

2,调用objc_msgSendSuper的方法,将这个结构体和setName的sel传递过去。函数里面在做的事情类似这样:从 objc_super结构体指向的superClass的方法列表开始找setName的selector,找到后再以 objc_super->receiver去调用这个selector,可能也会使用objc_msgSend这个函数,不过此时的第一个参数 theReceiver就是objc_super->receiver,第二个参数是从objc_super->superClass中找到 的selector

里面的调用机制大体就是这样了,以上面的分析,回过头来看开始的代码,当输出[self class]和[super class]时,是个怎样的过程。
当使用[self class]时,这时的self是PersonMe,在使用objc_msgSend时,第一个参数是receiver也就是self,也是 PersonMe* me这个实例。第二个参数,要先找到class这个方法的selector,先从PersonMe这个类开始找,没有,然后到PersonMe的父类 Person中去找,也没有,再去Person的父类NSObject去找,一层一层向上找之后,在NSObject的类中发现这个class方法,而 NSObject的这个class方法,就是返回receiver的类别,所以这里输出PersonMe。

当使用[super class]时,这时要转换成objc_msgSendSuper的方法。先构造objc_super的结构体吧,第一个成员变量就是self, 第二个成员变量是Person,然后要找class这个selector,先去superClass也就是Person中去找,没有,然后去Person 的父类中去找,结果还是在NSObject中找到了。然后内部使用函数objc_msgSend(objc_super->receiver, @selector(class)) 去调用,此时已经和我们用[self class]调用时相同了,此时的receiver还是PersonMe* me,所以这里返回的也是PersonMe。

Furthor more

在类的方法列表寻找一个方法时,还牵涉到一个概念类对象的isa指针和objc的meta-class概念,这里就不再详细介绍。

转自: http://web2.0coder.com

我们来 **详细展开“方式一”:使用 CocoaPods + 静态 Framework 开发 SDK,并集成 `InAppSettingsKit`**。 这是目前最成熟、最稳定、社区支持最好的方式,特别适合需要依赖 Objective-C 库(如 `InAppSettingsKit`)的 Swift 或混合语言 SDK。 --- ## 🎯 目标 构建一个名为 **MySettingsSDK** 的 SDK: - 功能:封装一键打开内嵌设置页的功能 - 依赖:使用 `InAppSettingsKit` 显示 Settings.bundle 内容 - 支持资源文件(如自定义的 Settings.bundle) - 可被其他 App 通过 **CocoaPods** 简单集成 - 构建为静态 Framework(推荐用于闭源组件) --- ## ✅ 第一步:项目结构设计 ``` MySettingsSDK/ ├── Sources/ │ ├── MySettingsManager.swift # 主要逻辑类 │ └── MyUtilities.swift # 辅助方法 ├── Resources/ │ └── Settings.bundle # 自定义设置项(可选) ├── Example/ # 示例 App(用于测试) │ └── MyApp/ │ └── ViewController.swift ├── MySettingsSDK.podspec # 核心:CocoaPods 配置文件 └── README.md ``` > 💡 提示:你可以用 `pod lib create MySettingsSDK` 自动生成这个结构。 --- ## ✅ 第二步:初始化项目(手动 or 自动生成) ### 方法 A:使用 CocoaPods 命令行工具自动创建 ```bash # 安装 cocoapods (如果还没安装) gem install cocoapods # 创建 SDK 模板 pod lib create MySettingsSDK ``` 执行后会提示你: - 使用 Swift 还是 Objective-C?→ 选 **Swift** - 是否需要示例工程?→ **Yes** - 测试框架?→ **Quick / None**(按需) - 是否基于 Git?→ **Yes** 它将自动生成完整的目录结构和 `.podspec` 文件。 --- ### 方法 B:手动创建(更灵活) 直接创建上述目录即可。重点是写好 `.podspec` 文件。 --- ## ✅ 第三步:编写核心代码(Sources/MySettingsManager.swift) ```swift // MySettingsManager.swift import Foundation import InAppSettingsKit // 来自外部依赖,由 CocoaPods 注入 public class MySettingsManager: NSObject { /// 单例 public static let shared = MySettingsManager() private override init() { } /// 打开内嵌设置页面 /// - Parameters: /// - from: 当前视图控制器 /// - completion: 展示完成后的回调 public func showSettings(from viewController: UIViewController, completion: (() -> Void)? = nil) { // 检查主 Bundle 是否存在 Settings.bundle guard let settingsBundlePath = Bundle.main.path(forResource: "Settings", ofType: "bundle") else { print("⚠️ Settings.bundle not found in main app bundle.") return } // 创建 IASK 控制器 let settingsViewController = IASKAppSettingsViewController() // 可选:只显示某些配置项 // settingsViewController.showCreditsFooter = false // 包装成导航控制器 let navController = UINavigationController(rootViewController: settingsViewController) // 弹出 viewController.present(navController, animated: true, completion: completion) } } ``` 📌 注意事项: - 不要在 SDK 中硬编码路径访问自己的 `Settings.bundle`,因为资源应由主 App 提供。 - 如果你想提供默认配置,见后续“资源打包”部分。 --- ## ✅ 第四步:编写 `.podspec` 文件 ```ruby # MySettingsSDK.podspec Pod::Spec.new do |s| s.name = 'MySettingsSDK' s.version = '1.0.0' s.summary = 'A lightweight SDK for showing in-app settings using InAppSettingsKit.' s.description = <<-DESC MySettingsSDK wraps InAppSettingsKit to easily display iOS settings inside your app. Supports custom Settings.bundle and seamless integration via CocoaPods. DESC s.homepage = 'https://github.com/yourusername/MySettingsSDK' s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'Your Name' => 'you@example.com' } s.source = { :git => 'https://github.com/yourusername/MySettingsSDK.git', :tag => s.version.to_s } s.ios.deployment_target = '11.0' s.swift_version = '5.0' # 源码路径 s.source_files = 'Sources/**/*.{swift,h,m}' # 公共头文件(如果有 OC 文件) # s.public_header_files = 'Sources/**/*.h' # 依赖 InAppSettingsKit s.dependency 'InAppSettingsKit', '~> 3.4' # 如果你的 SDK 包含资源文件(比如默认 Settings.bundle) s.resource_bundles = { 'MySettingsSDKResources' => ['Resources/*.bundle'] } # 必须设为 true,因为 InAppSettingsKit 是 Objective-C 框架 s.static_framework = true # 推荐设置:允许 Swift 动态库链接 s.pod_target_xcconfig = { 'BUILD_LIBRARY_FOR_DISTRIBUTION' => 'YES' } end ``` ### 🔍 关键字段解释: | 字段 | 说明 | |------|------| | `s.source_files` | 声明 Swift/OC 源码位置 | | `s.dependency 'InAppSettingsKit'` | 声明对外部库的依赖 | | `s.resource_bundles` | 打包资源(图片、plist、bundle),避免命名冲突 | | `s.static_framework = true` | ⚠️ 必须加!否则编译报错:“linker command failed with exit code 1” | | `BUILD_LIBRARY_FOR_DISTRIBUTION=YES` | 支持模块稳定性,适配不同 Swift 版本 | --- ## ✅ 第五步:添加 Settings.bundle(可选) 虽然通常由主 App 提供 `Settings.bundle`,但你可以提供一份默认模板。 ### 创建 Settings.bundle: 右键 → New File → `Settings Bundle` → 放入 `Resources/Settings.bundle` 编辑 `Root.plist` 添加条目,例如: ```xml <dict> <key>PreferenceSpecifiers</key> <array> <dict> <key>Type</key> <string>PSGroupSpecifier</string> <key>Title</key> <string>General</string> </dict> <dict> <key>Type</key> <string>PSTextFieldSpecifier</string> <key>Title</key> <string>Server URL</string> <key>Key</key> <string>server_url_preference</string> </dict> </array> </dict> ``` 然后在 `.podspec` 中已声明: ```ruby s.resource_bundles = { 'MySettingsSDKResources' => ['Resources/*.bundle'] } ``` 这样 CocoaPods 会在集成时自动复制资源。 > ⚠️ 注意:主 App 仍需有自己的 `Settings.bundle` 才能在系统设置中显示;IASK 读取的是主 App 的 bundle。 --- ## ✅ 第六步:验证与本地测试 ### 1. 在 Example 工程中测试功能 打开 `Example/MyApp/ViewController.swift`: ```swift import UIKit import MySettingsSDK // 或者 import MySettingsSDK class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white let button = UIButton(type: .system) button.setTitle("Open Settings", for: .normal) button.sizeToFit() button.center = view.center button.addTarget(self, action: #selector(openSettings), forControlEvents: .touchUpInside) view.addSubview(button) } @objc func openSettings() { MySettingsManager.shared.showSettings(from: self) } } ``` ### 2. 运行 Example App 确保: - 能弹出 `IASKAppSettingsViewController` - 设置项正确加载 - 无崩溃或链接错误 --- ## ✅ 第七步:发布到私有或公有 Specs Repo ### 方式 A:发布到 **CocoaPods Trunk**(公开) ```bash # 登录(首次) pod trunk register you@example.com 'Your Name' --description='MacBook Pro' # 验证 podspec pod lib lint MySettingsSDK.podspec --allow-warnings # 推送 pod trunk push MySettingsSDK.podspec --allow-warnings ``` 成功后,别人就可以: ```ruby pod 'MySettingsSDK' ``` --- ### 方式 B:私有 Spec Repo(企业内部使用) ```bash # 创建私有 Specs 仓库(一次) pod repo add MyPrivateSpecs https://github.com/yourcompany/specs.git # 推送到私有仓库 pod repo push MyPrivateSpecs MySettingsSDK.podspec --allow-warnings ``` 使用者 Podfile: ```ruby source 'https://github.com/yourcompany/specs.git' source 'https://cdn.cocoapods.org/' # 官方源 target 'MyApp' do pod 'MySettingsSDK' end ``` --- ## ✅ 第八步:文档与维护 ### 编写 README.md 示例: ```markdown # MySettingsSDK Show in-app settings seamlessly using InAppSettingsKit. ## Installation ```ruby pod 'MySettingsSDK' ``` ## Usage ```swift MySettingsManager.shared.showSettings(from: self) ``` > ⚠️ Make sure your app has a `Settings.bundle` in the main bundle. ``` --- ## ✅ 常见问题 & 解决方案 | 问题 | 原因 | 解决办法 | |------|------|----------| | `linker command failed with undefined symbols` | 没有正确链接 InAppSettingsKit | 确保 `.podspec` 中写了 `dependency` 并且 `static_framework = true` | | `No such module 'InAppSettingsKit'` | 头文件未暴露 | CocoaPods 会自动处理,不要手动改 Header Search Paths | | 资源找不到 | Bundle 查找路径错误 | 使用 `Bundle(for: Self.self)` 或让主 App 提供资源 | | 构建失败提示 “module compiled with Swift 5.x cannot be imported by Swift 5.y” | Swift 版本不一致 | 加上 `BUILD_LIBRARY_FOR_DISTRIBUTION=YES` | --- ## ✅ 总结:为什么这是最佳选择? | 优势 | 说明 | |------|------| | ✅ 完全支持 `InAppSettingsKit` | CocoaPods 原生支持 Objective-C 库 | | ✅ 支持资源管理 | `.bundle`、图片、xib 都能打包 | | ✅ 社区广泛 | 文档多,CI/CD 成熟 | | ✅ 易于调试 | 源码可见,断点可进 | | ✅ 支持多种集成方式 | 可与 SPM 共存(hybrid project) | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值