Skip to content

Latest commit

 

History

History
127 lines (101 loc) · 13.2 KB

Subclassing-In-Swift.md

File metadata and controls

127 lines (101 loc) · 13.2 KB

[CN]在 Swift 中继承本库的类

[EN]Subclassing the library's classes in Swift

中文 English
绑定的代码类 CYLTabBarController:CYLTabBarController/CYLTabBarController.h Bound to the class CYLTabBarController: CYLTabBarController/CYLTabBarController.h
绑定的代码类 CYLPlusButton:CYLTabBarController/CYLPlusButton.h Bound to the class CYLPlusButton: CYLTabBarController/CYLPlusButton.h
绑定的代码类 CYLBaseNavigationController:CYLTabBarController/CYLBaseNavigationController.h Bound to the class CYLBaseNavigationController: CYLTabBarController/CYLBaseNavigationController.h
绑定的代码类 CYLBaseViewController:CYLTabBarController/CYLBaseViewController.h Bound to the class CYLBaseViewController: CYLTabBarController/CYLBaseViewController.h
绑定的代码类 CYLBaseTableViewController:CYLTabBarController/CYLBaseTableViewController.h Bound to the class CYLBaseTableViewController: CYLTabBarController/CYLBaseTableViewController.h
中文 English
本库的 iOS 核心正在迁移到 Swift(Swift-Migration.md)。维护者已决定(2026-11-19 确认):CYLTabBarController、CYLPlusButton、CYLBaseNavigationController、CYLBaseViewController、CYLBaseTableViewController 与 CYLTabButtonBlockAnimation 将成为 open 的 Swift 类。Swift 类不能在 Objective-C 中被继承,因此 App 中继承这些类的子类需要用 Swift 编写。本文说明为什么、要改什么、以及如何一步一步完成。 The iOS core of this library is moving to Swift (Swift-Migration.md). The maintainer decided (confirmed 2026-11-19) that CYLTabBarController, CYLPlusButton, CYLBaseNavigationController, CYLBaseViewController, CYLBaseTableViewController and CYLTabButtonBlockAnimation become open Swift classes. A Swift class cannot be subclassed in Objective-C, so an app's subclasses of these classes must be written in Swift. This guide says why, what changes, and how to do it step by step.

[CN]目录

[EN]Contents

中文 English
状态 Status
为什么 Why
需要修改什么 What to change
示例 Examples
发布说明(TODO) Release notes (TODO)

[CN]状态

[EN]Status

中文 English
进行中:CYLPlusButton 自第 109 步起已是 Swift 类(尚未发布);其余类目前仍是 Objective-C,现有的 Objective-C 子类照常工作。它们在迁移第 ② 阶段变为 Swift 类(Swift-Migration.md,"Plan");该版本发布时,本文与发布说明会写明版本号。现在就可以按下文把子类改写为 Swift:同样适用于当前的 Objective-C 类,切换时无需再改。 In progress: CYLPlusButton is a Swift class since step 109 (not released yet); the other classes are still Objective-C today, and existing Objective-C subclasses of them keep working. They become Swift classes in phase ② of the migration (Swift-Migration.md, "Plan"); the release that ships it will be named here and in the release notes. You can port your subclasses to Swift now, as below: they also work with today's Objective-C classes, so nothing changes again at the switch.

[CN]为什么

[EN]Why

中文 English
Swift 把类导出给 Objective-C 时带有 objc_subclassing_restricted:Objective-C 可以使用 Swift 类,但不能继承它。最终目标是 Swift Package Manager 中只有一个 Swift target(Objective-C 外壳无法放进去),因此保留 Objective-C 外壳类的方案被替换为 open 的 Swift 类。 Swift exports its classes to Objective-C with objc_subclassing_restricted: Objective-C can use a Swift class but cannot subclass it. The goal is one Swift target in Swift Package Manager, which cannot hold Objective-C shell classes, so the earlier plan of thin Objective-C shells was replaced by open Swift classes.
不变的部分:Objective-C 代码仍然可以创建、配置并调用这些类(所有方法保留原来的 selector);只有"继承"需要 Swift。 What does not change: Objective-C code can still create, configure and call these classes (every method keeps its selector); only subclassing needs Swift.

[CN]需要修改什么

[EN]What to change

中文 English
1. 找出子类:在 App 中查找 @interface … : CYLTabBarController、: CYLPlusButton、: CYLBaseNavigationController、: CYLBaseViewController、: CYLBaseTableViewController、: CYLTabButtonBlockAnimation,以及这些子类的子类。 1. Find the subclasses: search the app for @interface … : CYLTabBarController, : CYLPlusButton, : CYLBaseNavigationController, : CYLBaseViewController, : CYLBaseTableViewController, : CYLTabButtonBlockAnimation, and for subclasses of those subclasses.
2. 让 App 支持 Swift:在 Objective-C 工程中添加第一个 Swift 文件时,Xcode 会询问是否创建桥接头文件(<App>-Bridging-Header.h),选择创建;在其中 #import <CYLTabBarController/CYLTabBarController.h>(CocoaPods / Carthage)或使用 import CYLTabBarController(Swift 文件中)。设置 SWIFT_VERSION(例如 5.0)。 2. Add Swift to the app: when you add the first Swift file to an Objective-C project, Xcode offers to create a bridging header (<App>-Bridging-Header.h); accept it and #import <CYLTabBarController/CYLTabBarController.h> there (CocoaPods / Carthage), or write import CYLTabBarController in the Swift files. Set SWIFT_VERSION (e.g. 5.0).
3. 逐个改写子类,保持类名:写成 @objc(CYLHomeViewController) class CYLHomeViewController: CYLBaseTableViewController。@objc(名称) 让 Objective-C 代码(以及 storyboard、NSClassFromString、配置文件中的类名)继续使用原来的名称。 3. Port each subclass, keeping its name: @objc(CYLHomeViewController) class CYLHomeViewController: CYLBaseTableViewController. @objc(Name) keeps the Objective-C name for Objective-C code, storyboards, NSClassFromString and class names in configuration files.
4. Objective-C 调用方导入生成的头文件:使用这些子类的 .m 文件 #import "<App模块名>-Swift.h",并删除旧的 .h。 4. Objective-C callers import the generated header: .m files that use the subclasses #import "<AppModule>-Swift.h"; delete the old .h.
5. PlusButton 的注册:Objective-C 子类常在 +load 中调用 [super registerPlusButton];Swift 没有 +load,请在创建 CYLTabBarController 之前显式调用 CYLPlusButtonSubclass.register()(例如在 application(_:didFinishLaunchingWithOptions:) 或根控制器中)。 5. Registering a plus button: Objective-C subclasses often call [super registerPlusButton] in +load; Swift has no +load, so call CYLPlusButtonSubclass.register() explicitly before creating the CYLTabBarController (e.g. in application(_:didFinishLaunchingWithOptions:) or the root view controller).
6. 重写的方法:- (void)viewDidLoad 变为 override func viewDidLoad();CYLPlusButtonSubclassing 的类方法(+plusButton、+indexOfPlusButtonInTabBar、+plusChildViewController 等)变为 static func(或 class func,若子类还要再重写)。 6. Overridden methods: - (void)viewDidLoad becomes override func viewDidLoad(); the class methods of CYLPlusButtonSubclassing (+plusButton, +indexOfPlusButtonInTabBar, +plusChildViewController, …) become static func (or class func when a subclass overrides them again).

| 7. 全局变量变为只读(第 109 步起):CYLExternPlusButton、CYLPlusChildViewController 与 CYLPlusButtonWidth 现在是 Swift 变量,Objective-C 通过宏读取;为它们赋值的代码不再能编译(它们只是库保存的值,读取不受影响)。 | 7. The globals are read-only (since step 109): CYLExternPlusButton, CYLPlusChildViewController and CYLPlusButtonWidth are Swift variables, read by Objective-C through macros; code that assigned them no longer compiles (they are values the library keeps; reading them is unaffected). | | 8. +multiplerInCenterY(已废弃)现在是 CYLPlusButtonSubclassing 的可选方法,不再是 CYLPlusButton 的方法:Swift 子类实现它时去掉 override。 | 8. +multiplerInCenterY (deprecated) is now an optional method of CYLPlusButtonSubclassing, no longer a method of CYLPlusButton: a Swift subclass that implements it drops override. | | 9. CYLTabBarItemImagePlaceholderWidth / …Height 变为只读(第 114 步起):在 Objective-C 中改用 CYLConstants.tabBarItemImagePlaceholderSize 设置。 | 9. CYLTabBarItemImagePlaceholderWidth / …Height are read-only in Objective-C (since step 114): set them with CYLConstants.tabBarItemImagePlaceholderSize. |

[CN]示例

[EN]Examples

中文 English
Objective-C(迁移前)与 Swift(迁移后)的 TabBarController 子类: A tab bar controller subclass in Objective-C (before) and Swift (after):
// Before: MainTabBarController.h / .m
@interface MainTabBarController : CYLTabBarController
@end

@implementation MainTabBarController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
}
@end
// After: MainTabBarController.swift (Objective-C still sees it as MainTabBarController)
@objc(MainTabBarController)
final class MainTabBarController: CYLTabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
    }
}
中文 English
PlusButton 子类(Swift)。在创建 TabBarController 之前调用一次 CYLPlusButtonSubclass.register(): A plus button subclass in Swift. Call CYLPlusButtonSubclass.register() once before the tab bar controller is created:
@objc(CYLPlusButtonSubclass)
class CYLPlusButtonSubclass: CYLPlusButton, CYLPlusButtonSubclassing {
    static func plusButton() -> Any {
        let button = CYLPlusButtonSubclass()
        button.setImage(UIImage(named: "post_normal"), for: .normal)
        button.sizeToFit()
        return button
    }

    static func indexOfPlusButtonInTabBar() -> UInt { 2 }
}
中文 English
本仓库自己的 Objective-C 子类(Objective-C 示例的 MainTabBarController、CYLFlatDesignMainTabBarController、CYLPlusButtonSubclass 及其子类、首页 / 消息 / 我的 / 同城 / 详情控制器,库中的 CYLConfigurationPlusButton,测试中的 CYLTestPlusButton、CYLSuperCallingTabBarController)按本文的步骤逐个改写,可作为参考(Swift-Migration.md 第 107 步起)。 This repository's own Objective-C subclasses (the Objective-C example's MainTabBarController, CYLFlatDesignMainTabBarController, CYLPlusButtonSubclass and its subclasses, the home / message / mine / same-city / details view controllers, the library's CYLConfigurationPlusButton, and the tests' CYLTestPlusButton and CYLSuperCallingTabBarController) are ported with these steps, one by one, and serve as references (Swift-Migration.md, from step 107).

[CN]发布说明(TODO)

[EN]Release notes (TODO)

中文 English
第 ② 阶段发布时加入发布说明的条目(版本号待定):"CYLTabBarController、CYLPlusButton、CYLBaseNavigationController、CYLBaseViewController、CYLBaseTableViewController 与 CYLTabButtonBlockAnimation 现在是 Swift 类。Objective-C 代码仍可使用它们,但其子类必须用 Swift 编写:见 docs/Subclassing-In-Swift.md。CYLTabBarBadgeView 已是 Swift 类,同样只能在 Swift 中继承。" The entry for the release notes when phase ② ships (version to be decided): "CYLTabBarController, CYLPlusButton, CYLBaseNavigationController, CYLBaseViewController, CYLBaseTableViewController and CYLTabButtonBlockAnimation are now Swift classes. Objective-C code can still use them, but subclasses must be written in Swift: see docs/Subclassing-In-Swift.md. CYLTabBarBadgeView is already a Swift class and can likewise be subclassed only in Swift."