Swift -《从0到1 - 2》:swift使用oc分类和swift创建分类

GitHub Demo 地址

在oc项目中,可以通过分类对原有类进行扩展
但是在swift项目中,没有分类这种概念,相对应的有扩展Extensions

下面是swift中扩展(Extensions)的说明

扩展就是向一个已有的类、结构体、枚举类型或者协议类型添加新功能(functionality)。这包括在没有权限获取原始源代码的情况下扩展类型的能力(即逆向建模)。扩展和 Objective-C 中的分类(categories)类似。(不过与 Objective-C 不同的是,Swift 的扩展没有名字。)

所以在swift开发中,我们可以通过桥接头文件直接使用oc的分类,或者通过Extensions这种方式写swift语法的类扩展

swift调用oc分类方法

有两种方式创建桥接文件

1、自动创建:直接导入OC文件,这时会提示创建桥接文件
2、手动创建:新建文件选择创建一个头文件Header File,命名格式为yourprojectname-Bridging-Header.h(也可自定义名字)
在这里插入图片描述

在这里插入图片描述

然后build setting 搜索 objective-c bridging Header 设置头文件路径:

$(SRCROOT)/项目内桥接文件放置的路径

设置路径之后,在头文件内引用导入的分类头文件

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import <UIKit/UIKit.h>

#import "UIView+JhExtension.h"
#import "UIImage+JhExtension.h"
#import "UIColor+JhExtension.h"

到此就设置完成,可在swift文件内测试一下是否导入成功

比如我oc代码中这样一个分类

/// color:支持@“#123456”、 @“0X123456”、 @“123456”三种格式
+ (UIColor*)Jh_hexColor:(NSString*)hex alpha:(CGFloat)alpha;

在swift文件这样使用

self.view.backgroundColor = UIColor.jh_hexColor("#21AF64", alpha: 0.5)

swift实现Extensions

这里以UIBarButtonItem分类为例,此分类实现创建文字和图片UIBarButtonItem(导航条左右item)

用法

	let textItem = UIBarButtonItem.Jh_textItem(title: "文字", titleColor: UIColor.red, target: self, action: #selector(ClickItem))
	let imgItem =  UIBarButtonItem.Jh_imageItem(imageName: "ic_close_black", target: self, action: #selector(ClickItem))
	self.navigationItem.leftBarButtonItem = textItem
	self.navigationItem.rightBarButtonItem = imgItem

    @objc func ClickItem() {
        NSLog("点击事件")
        self.navigationController?.pushViewController(SystemUIViewController(), animated: true)
    }

在这里插入图片描述

全部代码

import UIKit

extension UIBarButtonItem {
    
    /// 根据图片生成UIBarButtonItem
    class func Jh_imageItem(imageName:String,target:AnyObject,action:Selector) -> UIBarButtonItem {
        return Jh_customItem(title: "", titleColor: UIColor.white, imageName: imageName, target: target, action: action,contentHorizontalAlignment: .center)
    }
    
    /// 根据文字生成UIBarButtonItem
    class func Jh_textItem(title:String,titleColor:UIColor,target:AnyObject,action:Selector) -> UIBarButtonItem {
        return Jh_customItem(title: title, titleColor: titleColor, imageName: "", target: target, action: action,contentHorizontalAlignment: .center)
    }
    
    /// 返回按钮 带箭头的
    class func Jh_backItem(imageName:String,target:AnyObject,action:Selector) -> UIBarButtonItem {
        return Jh_customItem(title: "", titleColor: UIColor.white, imageName: imageName, target: target, action: action,contentHorizontalAlignment: .left,isBack: true)
    }
    
    /// 快速初始化一个UIBarButtonItem,内部是按钮
    class func Jh_customItem(title:String,titleColor:UIColor,imageName:String,target:AnyObject,action:Selector,
                             contentHorizontalAlignment:UIControl.ContentHorizontalAlignment,isBack:Bool=false) -> UIBarButtonItem {
        let button = UIButton()
        if (title.count>0) {
            button.setTitle(title, for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        }
        if (imageName.count>0) {
            button.setImage(UIImage(named: imageName), for: .normal)
            button.setImage(UIImage(named: imageName), for: .highlighted)
        }
        button.setTitleColor(titleColor, for: .normal)
        button.setTitleColor(titleColor.withAlphaComponent(0.5), for: .highlighted)
        button.setTitleColor(titleColor.withAlphaComponent(0.5), for: .disabled)
        button.addTarget(target, action: action, for: .touchUpInside)
        if (isBack) {
            button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        } else {
            button.sizeToFit()
        }
        button.contentHorizontalAlignment = contentHorizontalAlignment;
        return UIBarButtonItem(customView: button)
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值