Swift 仍然是一门全新的语言,它往往并不能免除开发者编写大量的样板代码。Sirius 框架的主要目标是简化现代编程中对基本事物(对象)的操作。框架包含了一系列对 Swift 的原生类型和协议的扩展,这使得编程变得更加容易,并允许节省大量的代码。
Sirius
文件夹的内容复制到您的项目中。或者
Sirius
cocoapod注意:对于 Swift 2.x 使用 Sirius v1.0
。对于 Swift 3.0 使用 Sirius v3.0
。
let object = SomeClass()
object.use { (object) in
// Do several operations with object...
}
object.useAs(NSString.self) { (object) in
// Do something with object casted to NSString type...
}
两种方法 use
和 useAs
都支持链式调用,因此可以写出类似以下的内容
object.use { (object) in
// Do several operations with object...
}.useAs(NSString.self) { (object) in
// Do something with object casted to NSString type...
}.useAs(NSNumber.self) { (object) in
// Do something with object casted to NSNumber type...
}
由于上述两种方法都返回接收者的实例,因此可以像这样使用它们与初始化器配合使用
let view = UIView().use { (object) in
object --> .blueColor()
object.clipsToBounds = true
}
这种方法可以使代码更加清晰易懂。
let classNameIncludingNamespace = SomeClass.className(includeNamespace: true) // "com.domain.appName.SomeClass"
let classNameWithoutNamespace = SomeClass.className(includeNamespace: false) // "SomeClass"
通常,当您想要设置视图的层,例如使角落变得圆润,您会编写如下代码
view.layer.backgroundColor = UIColor.whiteColor().CGColor
view.layer.borderColor = UIColor.blackColor().CGColor
view.layer.borderWidth = 1.0
view.layer.cornerRadius = view.bounds.size.width / 2.0
view.layer.masksToBounds = true
使用 Sirius
,您可以忘记在每一行中的 view.
输入
view.layer.use { (layer) in
layer.backgroundColor = UIColor.whiteColor().CGColor
layer.borderColor = UIColor.blackColor().CGColor
layer.borderWidth = 1.0
layer.cornerRadius = view.bounds.size.width / 2.0
layer.masksToBounds = true
}
同样,您也可以在视图初始化时这样做
let view = UIView().use { (view) in
view.frame = CGRect(x: 0.0, y: 0.0, width: 200.0, height: 200.0)
view.layer.use({ (layer) in
layer.backgroundColor = UIColor.whiteColor().CGColor
layer.borderColor = UIColor.blackColor().CGColor
layer.borderWidth = 1.0
layer.cornerRadius = view.bounds.size.width / 2.0
layer.masksToBounds = true
})
}
Sirius
根据 MIT 许可证提供。有关更多信息,请参阅 LICENSE
文件。