Install via Carthage
github "atsushi130/SwiftExtensions"
- Swift 4 or later
- NSObject
- Int
- Double
- CGFloat
- String
- Bool
- Date
- DateFormatter
- JSONEncoder
- JSONDecoder
- JSONCoder
- CGColor
- UIColor
- UITextView
- UICollectionView
- UITableView
- UIView
- UUID
- NibDesignable
- NibInstantiatable
Get class name.
let view = CustomView()
print(view.ex.className)
Half
let number = 3
print(number.ex.half) // 1.5
Half
let number = 3.0
print(number.ex.half) 1.5
Floor
let number = 1.4
print(number.ex.floor) // 1.0
Ceil
let number = 1.4
print(number.ex.ceil) // 2.0
Round
let number = 1.5
print(number.ex.round) // 2.0
CGFloat
let number = 1.5.ex.cgFloat
The same as String extensions.
attributed
"string".ex.attributed
toDate
"2018/01/01 00:00:00".ex.toDate()
Regular expression
password.ex.isMatch(pattern: "^(?=.*[a-z])(?=.*[$@$#!%*?&])[A-Za-z\\d$@$#!%*?&]{8,}$")
snakecased
"stringString".snakecased() // "string_string"
kebabcased
"stringString".kebabcased() // "string-string"
toInt
print(true.ex.toInt) // 1
toString
let dateString = Date().ex.toString()
let formatter = DateFormatter.ex.from(locale: Local.current, format: "yyyy/MM/dd HH:mm:ss")
let encoder = JSONEncoder.snakeCaseEncoder
let decoder = JSONDecoder.snakeCaseDecoder
let encoder = JSONCoder.snakeCaseEncoder
let decoder = JSONCoder.snakeCaseDecoder
to UIColor
view.backgroundColor = cgColor.ex.uiColor
let color = UIColor.ex.hex(hex: 0xAABBCC)
let color = UIColor.ex.hex(hexString: "ffffff")
let textView = UITextView()
textview.ex.placeholder = "Input message"
Custom cell registration.
@IBOutlet private weak var collectionView: UICollectionView! {
didSet {
self.layout = UICollectionViewFlowLayout()
self.collectionView.collectionViewLayout = self.layout
self.collectionView.ex.register(cellType: CustomCell.self)
self.collectionView.ex.register(reusableViewType: CustomReusableView.self)
self.collectionView.dataSource = self
self.collectionView.delegate = self
}
}
Other example.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return collectionView.ex.dequeueReusableCell(with: CustomCell.self, for: indexPath)
}
safeAreaInsets
// ios10.x or less: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
let safeAreaInsets = self.view.ex.safeAreaInsets
fillSuperview
let view = UIView()
superView.addSubView(view)
view.ex.fillSuperview()
UUID.ex.generate()
Setup the File’s Owner with the custom class you created.
conform to NibDesignable. Please call configureNib
method on init(frame:)
and init?(decoder:)
.
@IBDesignable
final class ReactiveView: UIView, NibDesignable {
init(frame: CGRect) {
super.init(frame: frame)
self.configureNib()
}
required init?(decoder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.configureNib()
}
}
Last step, set its class as custom view (ex: ReactiveView) in storyboard.
final class CustomView: NibInstantiatable { ... }
let customView = CustomView.instantiate() // create instance from CustomView.Xib
SwiftExtensions is available under the MIT license. See the LICENSE file.