Code Monkey home page Code Monkey logo

swifttargetedextensions's Introduction

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.

swifttargetedextensions's People

Contributors

atsushi130 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

multinerd-forks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.