Skip to content

Latest commit

 

History

History
241 lines (202 loc) · 5.02 KB

File metadata and controls

241 lines (202 loc) · 5.02 KB

SwiftExtensions

Installation

Install via Carthage

github "atsushi130/SwiftExtensions"

Requirements

  • Swift 4 or later

Extensions and Protocols Usage

NSObject

Get class name.

let view = CustomView()
print(view.ex.className)

Int

Half

let number = 3
print(number.ex.half) // 1.5

Double

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

CGFloat

The same as String extensions.

String

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"

Bool

toInt

print(true.ex.toInt) // 1

Date

toString

let dateString = Date().ex.toString()

DateFormatter

let formatter = DateFormatter.ex.from(locale: Local.current, format: "yyyy/MM/dd HH:mm:ss")

JSONEncoder

let encoder = JSONEncoder.snakeCaseEncoder

JSONDecoder

let decoder = JSONDecoder.snakeCaseDecoder

JSONCoder

let encoder = JSONCoder.snakeCaseEncoder
let decoder = JSONCoder.snakeCaseDecoder

CGColor

to UIColor

view.backgroundColor = cgColor.ex.uiColor

UIColor

let color = UIColor.ex.hex(hex: 0xAABBCC)
let color = UIColor.ex.hex(hexString: "ffffff")

UITextView

let textView = UITextView()
textview.ex.placeholder = "Input message"

uitextview placeholder

UICollectionView

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)
}

UIView

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

UUID.ex.generate()

NibDesignable

Setup the File’s Owner with the custom class you created. image

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. image

NibInstantiatable

final class CustomView: NibInstantiatable { ... }
let customView = CustomView.instantiate() // create instance from CustomView.Xib

License

SwiftExtensions is available under the MIT license. See the LICENSE file.