Latest 0.4.4
Homepage https://github.com/derekcoder/SwiftDevHints
License MIT
Platforms ios 10.0
Authors

English

Version
License
Platform

Swift日常开发工具集

功能

  • [x] 随机数生成
  • [x] 格式化输出
  • [x] 浮点型数据的四舍五入
  • [x] 改造UserDefaults
  • [x] 改造print函数
  • [x] 改造UIColor
  • [x] 更优雅的实现Segue
  • [x] 改造Optional
  • [x] CircularImageView
  • [x] 快速读取Info.plist
  • [ ] 自定义一些常用函数
    • [x] accumulate
    • [x] last
    • [x] all
  • [ ] Base64 Encoding and Decoding
  • [ ] Generic Table View Controller
  • [ ] Nib Register for UITableViewCell
  • [ ] Convenient Methods for Adding Constraints
  • [ ] Networking

环境要求

  • iOS 9.0+
  • Swift 4 (SwiftDevHints 0.4.x), Swift 3 (SwiftDevHints 0.3.x)

安装

CocoaPods

pod 'SwiftDevHints'

Swift 3

pod 'SwiftDevHints', '~> 0.3.0'

使用说明

随机数生成

Swift开发小技巧系列 – 随机数生成

import SwiftDevHints

let randomInt = (0...10).randomInt // randomInt is Int number between 0 ~ 10 (include 10)
let randomInt = (0..<10).randomInt // randomInt is Int number between 0 ~ 10 (not include 10)

格式化输出

Swift开发小技巧系列 – 格式化输出

import SwiftDevHints

let aInt = 8
let formatedInt1 = aInt.format("03") // "008"
let formatedInt2 = aInt.format("3") // "  8"
import SwiftDevHints

let aDouble = 3.14159265
let formatedDouble = aDouble.format(".2") // "3.14"
let formatedDouble = aDouble.format(".3") // "3.142"

浮点型数据的四舍五入

Swift开发小技巧系列 – 浮点型数据的四舍五入

import SwiftDevHints

let aDouble = 3.14159265
let roundedDouble1 = aDouble.roundTo(places: 2) // 3.14
let roundedDouble2 = aDouble.roundTo(places: 3) // 3.142

let aFloat: Float = 3.14159265
let roundedFloat1 = aFloat.roundTo(places: 2) // 3.14
let roundedFloat2 = aFloat.roundTo(places: 3) // 3.142

改造UserDefaults

Swift开发小技巧系列 – 改造UserDefaults

import SwiftDevHints

extension UserDefaults.Name {
    static let Username: UserDefaults.Name = "SwiftDevHints-Demo.Username"
    static let Password: UserDefaults.Name = "SwiftDevHints-Demo.Password"
}

UserDefaults.standard.set("Derek", forName: .Username)
UserDefaults.standard.set("Password", forName: .Password)

let username = UserDefaults.standard.string(forName: .Username)
let password = UserDefaults.standard.string(forName: .Password)

改造print函数

Swift开发小技巧系列 – 改造print函数

  • 支持显示输出语句的所在的文件,函数和行数
  • 支持不输出任何信息
  • Release模式下自动失效。
import SwiftDevHints

// PrintHelperViewController.swift
func testPrintLog() {
    debugPrintLog("Hello, World!")
}

// 输出结果: PrintHelperViewController.swift:testPrintLog():20 -> Hello, World!
// PrintHelperViewController.swift
func testPrintLog() {
    debugPrintLog()
}
// 输出结果: PrintHelperViewController.swift:testPrintLog():20

改造UIColor

Swift开发小技巧系列 – 改造UIColor

  • 支持直接以0 ~ 255 之间的RGB值来初始化UIColor
  • 支持直接以十六进制长度RGB值初始化UIColor
  • 可以直接根据UIColor对象得到各种格式的RGB值
// Initialize UIColor with RGB based 255 (255 32 171)
let color = UIColor(redIn255: 255, greenIn255: 32, blueIn255: 171)

// Initialize UIColor with RGB Hex String
let color = UIColor(hex: "FF20AB") 
// let color = UIColor(hex: "ff20ab") 
// let color = UIColor(hex: "#FF20AB") 
// let color = UIColor(hex: "#ff20ab")

// Get RGBA based 1.0 from a color
let rgba = color.rgba // (red 1.0, green 0.125490196078431, blue 0.670588235294118, alpha 1.0)

// Get RGB based 255 and A base 100 from a color
let intRGBA = color.intRGBA // (red 255, green 32, blue 171, alpha 100)

let rgbHexString = color.rgbHexString(prefix: "#") // "#FF20AB"
// let rgbHexString = color.rgbHexString() // "FF20AB"

更优雅的实现Segue

Swift开发小技巧系列 – 更优雅的实现Segue

class TestCustomSegueViewController: UITableViewController, CustomSegueProtocol {
    enum CustomSegueIdentifier: String {
        case showNext
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performCustomSegue(.showNext, sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        switch customSegueIdentifier(forSegue: segue) {
        case .showNext:
            // Configuration for next page
        }
    }
}

改造Optional

Swift开发小技巧系列 – 改造Optional

guard let text = textField.text.nilIfEmpty else {
// handle for nil or empty
return
}
// Do something with text
let names = ["Derek", "", "John", "", "Tony", nil]   // [String?]

let validNames1 = names.map { $0.nilIfEmpty }  // [String?]
print(validNames1) // [Optional("Derek"), nil, Optional("John"), nil, Optional("Tony"), nil]

let validNames2 = names.flatMap { $0.nilIfEmpty } // [String]
print(validNames2)  // ["Derek", "John", "Tony"]

CircularImageView

Using in Storyboard

  • Marked your UIView as CircularImageView in Xcode
  • You can change image and saturation properties directly in Xcode

Using programmatically

let circularImageView = CircularImageView(frame: frame)

快速读取Info.plist

let bundle = Bundle.main

let displayName = bundle.displayName  // String?
let identifier = bundle.identifier    // String?
let version = bundle.version          // String?
let build = bundle.build              // String?

// 所有已经定义的可以快速访问的Key
private enum InfoPlistKey: String {
case name = "CFBundleName"
case displayName = "CFBundleDisplayName"
case developmentRegion = "CFBundleDevelopmentRegion"
case identifier = "CFBundleIdentifier"
case version = "CFBundleShortVersionString"
case build = "CFBundleVersion"
case packageType = "CFBundlePackageType"
}

自定义一些常用函数

// accumulate
let nums = [1, 2, 3, 4]
nums.accumulate(0, +)  // [1, 3, 6, 10]

// last
let names = ["Derek", "Tony", "Jogn"]
names.last { $0.hasPrefix("De") }  // Optional("Derek")

// all
let evenNums = [2, 4, 6, 8, 10]
evenNums.all { $0 % 2 == 0 }  // true

联系方式

你可以在 Twitter关注和联系我. 如果你有任何问题和建议,也非常欢迎 open a ticket

版权

SwiftDevHints is released under the MIT license. See LICENSE for details.

Latest podspec

{
    "name": "SwiftDevHints",
    "version": "0.4.4",
    "summary": "A very useful set of development tools.",
    "description": "SwiftDevHints is a very useful set of development tools.",
    "homepage": "https://github.com/derekcoder/SwiftDevHints",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "derekcoder": "[email protected]"
    },
    "source": {
        "git": "https://github.com/derekcoder/SwiftDevHints.git",
        "tag": "0.4.4"
    },
    "platforms": {
        "ios": "10.0"
    },
    "source_files": [
        "SwiftDevHints/Sources/*.swift",
        "SwiftDevHints/SwiftDevHints.h"
    ],
    "pod_target_xcconfig": {
        "SWIFT_VERSION": "4.0"
    },
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This