TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jan 2016 |
SPMSupports SPM | ✓ |
Maintained by Jo Albright.
A new design pattern to allow closure initialization.
To run the example project, clone the repo, and run pod install
from the Example directory first.
The desire is to initialize within a closure for unique customization while allowing the ability to use constants.
Previous Code
// needs to be "var" to allow append below
var randomNumbersArray: [Int] = []
for _ in 0...10 {
randomNumbersArray.append(Int(arc4random_uniform(100)))
}
New Code
// can now use let and append within initializer closure
let randomNumbersArray: [Int] {
for _ in 0...10 {
$0.append(Int(arc4random_uniform(100)))
}
}
struct Person: Inlinit {
var age: Int = 0
var name: String?
}
// initialize & set properties
var me = Person {
$0.name = "Jo"
$0.age = 32
}
// update properties
me <- {
$0.age = 30
$0.name = "John"
}
func newLabel(text: String) -> UILabel {
let label: UILabel = UILabel(frame: CGRect(x: 10, y: 10, width: 100, height: 40))
label.text = text
label.textColor = UIColor.magentaColor()
return label
}
newLabel("This is lame...")
New Code
func newLabel(text: String) -> UILabel {
return UILabel {
$0.frame = CGRect(x: 10, y: 10, width: 100, height: 40)
$0.textColor = UIColor.magentaColor()
}.text = text
}
newLabel("This is Awesome!")
So many possibilities unlocked with this new design pattern
Inlinit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Inlinit"
Inlinit is also available through Swift Package Manager. Please take a look at the link to learn more about how to use SPM.
import PackageDescription
let package = Package(
name: "YOUR_PACKAGE_NAME",
dependencies: [
.Package(url: "https://github.com/joalbright/Inlinit.git", majorVersion: 0)
]
)
Inlinit is available under the MIT license. See the LICENSE file for more info.