atsushi130 / SwiftExtensions

Extensions for Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.className)

Int

Half

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

Double

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

CGFloat

The same as String extensions.

String

attributed

"string".attributed

toDate

"2018/01/01 00:00:00".toDate()

Regular expression

password.isMatch(pattern: "^(?=.*[a-z])(?=.*[$@$#!%*?&])[A-Za-z\\d$@$#!%*?&]{8,}$")

Bool

toInt

print(true.toInt) // 1

Dictionary

operator +

["key1": "value1"] + ["key2": "value2"] // ["key1": "value1", "key2": "vaule2"]

Array

Nullable element

let array = [1, 2, 3]
array[ifAny: 4] // .none

Date

toString

let dateString = Date().toString()

DateFormatter

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

CGColor

to UIColor

view.backgroundColor = cgColor.uiColor

UIColor

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

UITextView

let textView = UITextView()
textview.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.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)
}

UIView

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

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

CaseIterable

enum License: CaseIterable {
    case free
    case enterprise
}

print(License.allCases) // [License.free, License.enterprise]

License

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

About

Extensions for Swift

License:MIT License


Languages

Language:Swift 97.8%Language:Objective-C 2.2%