Cooking 1.1.2

Cooking 1.1.2

测试已测试
语言语言 SwiftSwift
许可 Apache-2.0
发布最后发布Sep 2019
SPM支持 SPM

Shane Qi 维护。



Cooking 1.1.2

GitHub Repo

Cook classes/structs with the taste you love.
Naming your extensions in the Swifty way people love.

Cooking

extension 是 Swift 的强大功能。我们可以扩展自己的 class/struct,也可以扩展任何框架的 class/struct

框架 之前 现在
RxSwift label.rx_text label.rx.text
SnapKit view.snp.pinConstraints view.snp.makeConstraints

Cooking 是帮助您采用这种 swift 风格命名 extension 的库。

用法

必须让 classstruct 遵守 Edible 协议,才能有 .cook 的包装。

然后使用 where 限制扩展 Cooking 以扩展您的 classstruct。您必须使用 ingredient 来引用实例,而不是使用 self

对于 class,约束可以使用 ==:
对于 struct,约束必须使用 ==

import Cooking

extension String: Edible {}
//  Use `==` for structs.
extension Cooking where Ingredient == String {

	static func bitterOne() -> String {
		return "😭😭😭"
	}

	func sweeten() -> String {
		return ingredient.replacingOccurrences(of: "😭", with: "😋")
	}
}

let bitterString = String.cook.bitterOne()
//  bitterString = "😭😭😭"
let sweetString  = bitterString.cook.sweeten()
//  sweetString  = "😋😋😋"

extension UIView: Edible {}
extension Cooking where Ingredient: UIView {
	func area() -> CGFloat {
		return ingredient.frame.width * ingredient.frame.height
	}
}

let view = UIView(frame: .init(origin: .zero, size: .init(width: 50, height: 20)))
let area = view.cook.area()
//  area = 1000

安装

CocoaPods

CocoaPods是为Cocoa项目提供的依赖管理器。您可以使用以下命令安装它:

$ gem install cocoapods

要使用CocoaPods在您的Xcode项目中使用Cooking,请在Podfile中指定它:

pod 'Cooking'

然后,运行以下命令:

$ pod install

Swift Package Manager

在Package.swift文件中将此项目作为依赖项添加。

.Package(url: "https://github.com/ShaneQi/Cooking.git", majorVersion: 1, minor: 0)

手动方式

将文件Sources/Cooking.swift包含在您的项目中。

限制

  1. 不能使用mutating func扩展结构体。
extension String: Edible {}

extension Cooking where Ingredient == String {

	mutating func addExclamation() {
		ingredient.append("!")
	}

}

"hello".cook.addExclamation()
// ERROR: ^ Cannot use mutating member, `cook` is a get-only property.
  1. 不能扩展泛型类型。
extension Optional: Edible {}  
                               
extension Cooking where Ingredient == Optional {
//                                ERROR: ^ Reference to generic type 'Optional' requires arguments in <...>                          

	func someFunction() {}

}

授权

Apache-2.0