Install via Carthage
github "atsushi130/SwiftExtensions"
- Swift 4 or later
- NSObject
- Int
- Double
- CGFloat
- String
- Bool
- Dictionary
- Array
- Date
- DateFormatter
- CGColor
- UIColor
- UITextView
- UICollectionView
- UITableView
- UIView
- UUID
- NibDesignable
- NibInstantiatable
- CaseIterable
Get class name.
let view = CustomView()
print(view.className)
Half
let number = 3
print(number.half) // 1.5
Half
let number = 3.0
print(number.half) 1.5
Floor
let number = 1.4
print(number.floor) // 1.0
Ceil
let number = 1.4
print(number.ceil) // 2.0
Round
let number = 1.5
print(number.round) // 2.0
CGFloat
let number = 1.5.cgFloat
The same as String extensions.
attributed
"string".attributed
toDate
"2018/01/01 00:00:00".toDate()
Regular expression
password.isMatch(pattern: "^(?=.*[a-z])(?=.*[$@$#!%*?&])[A-Za-z\\d$@$#!%*?&]{8,}$")
toInt
print(true.toInt) // 1
operator +
["key1": "value1"] + ["key2": "value2"] // ["key1": "value1", "key2": "vaule2"]
Nullable element
let array = [1, 2, 3]
array[ifAny: 4] // .none
toString
let dateString = Date().toString()
let formatter = DateFormatter.from(locale: Local.current, format: "yyyy/MM/dd HH:mm:ss")
to UIColor
view.backgroundColor = cgColor.uiColor
let color = UIColor.hex(hex: 0xAABBCC)
let color = UIColor.hex(hexString: "ffffff")
let textView = UITextView()
textview.placeholder = "Input message"
Custom cell registration.
@IBOutlet private weak var collectionView: UICollectionView! {
didSet {
self.layout = UICollectionViewFlowLayout()
self.collectionView.collectionViewLayout = self.layout
self.collectionView.register(cellType: CustomCell.self)
self.collectionView.register(reusableViewType: CustomReusableView.self)
self.collectionView.dataSource = self
self.collectionView.delegate = self
}
}
Other example.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCell(with: CustomCell.self, for: indexPath)
}
safeAreaInsets
// ios10.x or less: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
let safeAreaInsets = self.view.safeAreaInsets
fillSuperview
let view = UIView()
superView.addSubView(view)
view.fillSuperview()
UUID.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
enum License: CaseIterable {
case free
case enterprise
}
print(License.allCases) // [License.free, License.enterprise]
SwiftExtensions is available under the MIT license. See the LICENSE file.