Skip to content

Starkrimson/Oh.Swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

33 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Oh.Swift

Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Extension

// UIColor
view.backgroundColor = .oh.hex(0xFFFFFF)
view.backgroundColor = .oh.random


// UITableView
tableView.oh.register(UITableViewCell.self)
// ...
let cell = tableView.oh.dequeue(UITableViewCell.self)

Property Wrapper

// UserDefaultsWrapper 

import OhSwift

@UserDefaultsWrapper("oh.swift.toggle", defaultValue: true)
var toggle: Bool!

// UserDefaults.standard.bool(forKey: "oh.swift.toggle")
"toggle \(toggle!)"

// UserDefaults.standard.set(false, forKey: "oh.swift.toggle")
// UserDefaults.standard.synchronize()
toggle = false
// CodableDefaultWrapper


struct Model: Codable {
    @Default<String> var text: String
    @Default<Int> var integer: Int
    @Default<Double> var decimal: Double
    @Default.True var flag: Bool
}

// ...

let jsonString = """
                 {
                    "text1": "hello world!",
                    "integer": 99,
                    "decimal": 66
                 }
                 """
do {
    let model = try JSONDecoder().decode(Model.self, from: jsonString.data(using: .utf8)!)
    print(model) // text = "", integer = 99, decimal = 66.0, flag = true
} catch {
    print(error.localizedDescription)
}

Modifier

let testView = UIView.oh.new { make in
    make.background(color: .white)
        .border(width: 10)
        .corner(radius: 20)
    if #available(iOS 11.0, *) {
        make.corner(radius: 20, corners: [.topLeft, .bottomRight])
    }
    make.rawValue.frame = .init(x: 50, y: 650, width: 100, height: 100)
}

UIButton(frame: .init(x: 310, y: 650, width: 100, height: 100))
    .oh.modifier
    .superView(view)
    .title("Button")
    .titleColor(.oh.random)
    .font(ofSize: 20, weight: .semibold)
    .action(for: .touchDown) {
        print("...")
    }

RxSwift Extension

/// disposeBag
button.rx.tap
    .subscribe()
    .disposed(by: rx.disposeBag)

/// ็ฎ€ๅŒ– items(cellIdentifier:,cellType:)
/// ๅฏ้…ๅˆ tableView.oh.register(UITableViewCell.self) ไฝฟ็”จ
Observable.just(dataSource.map { $0.items }.flatMap { $0 } )
    .bind(to: tableView.rx.items(cell: UITableViewCell.self)) { (row, element, cell) in
        cell.textLabel?.text = element
    }
    .disposed(by: rx.disposeBag)

/// ๅœจๅŽŸ modelSelected ๅŸบ็ก€ไธŠ่ฟ”ๅ›ž IndexPath
tableView.rx.modelSelectedAtIndexPath(String.self)
    .subscribe(onNext: { (element, indexPath) in
        print("\(element) \(indexPath)")
    })
    .disposed(by: rx.disposeBag)

More...

Installation

Oh.Swift is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Oh.Swift'

# Install RxSwift extension
pod 'Oh.Swift/Rx'

Author

Starkrimson, starkrimson@icloud.com

License

Oh.Swift is available under the MIT license. See the LICENSE file for more info.

About

๐Ÿš€ Swift extension and utils.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published