Codemine 0.1.0

Codemine 0.1.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2016年2月
SPM支持 SPM

Maintained by Nodes Agency.



Codemine 0.1.0

Codemine

Codemine 是一个包含对 Swift 项目有用的函数和语法糖的扩展集合。

📝要求

  • iOS 8.0+

📦安装

Swit 软件包管理器

要将 Codemine 作为 Swift 包管理器 包使用,只需将以下代码添加到您的 Package.swift 文件中。

import PackageDescription

let package = Package(
    name: "YourPackage",
    dependencies: [
        .Package(url: "https://github.com/nodes-ios/Codemine.git", majorVersion: 0)
    ]
)

注意:目前这不会工作,因为 SPM 不支持 iOS,但我们将在它支持后立即支持它! :)

💻用法

应用程序

let appName = Application.name             // CFBundleDisplayName : String
let appVersion = Application.version       // CFBundleShortVersionString : String
let appExecutable = Application.executable // CFBundleExecutable : String
let appBundle = Application.bundle         // CFBundleIdentifier : String
let appSchemes = Application.schemes       // CFBundleURLSchemes : [String]
let mainAppScheme = Application.mainScheme // CFBundleURLSchemes.first : String?

轻松访问主包信息。

CGPoint

let point1 = CGPoint(x: 5, y: 5)
let point2 = CGPoint(x: 5, y: 6)
print(point1.isCloseTo(point2, tolerance: 1))   // true
print(point1.isCloseTo(point2, tolerance: 0.5)) // false
print(point1+point2)    // (10, 11)
print(point1*point2)    // (25, 30)
print(point1*2)         // (10, 10)
print(point1-point2)    // (0, -1)
print(point1/point2)    // (1, 0.83)
print(point1/5)         // (1, 1)

CGRect

var rect = CGRect(x: 10, y: 10, width: 120, height: 100)
rect.x = 50
print(rect) // outputs x:50, y:20, width: 120, height:100
rect.y = -10
print(rect) // outputs x:50, y:-10, width: 120, height:100
let reversedRect = rect.rectByReversingSize()
print(reversedRect) // outputs x:50, y:-10, width:100, height:120

CGSize

let size1 = CGSize(width: 20, height: 40)
let size2 = CGSize(width: 121, height: 576)
print(size1+size2)      // CGSize(width: 141, height: 616)

Grand Central Dispatch

dispatch {
  // dispatch in main queue
}

dispatch(queue: .Background) {
  // dispatch in background queue
}

lazy var serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL)
dispatch(queue: .Custom(serialQueue)) {
  // dispatch in a serial queue
}

使用 Grand Central Dispatch 容易地进行调度。支持所有常规的全局队列:MainInteractiveInitiatedUtilityBackground。以及用于您自己的调度队列的:.Custom()

NSError

let error = NSError(domain: domain, code: code, description: description)

而不是

let error = NSError(domain: domain, code: code, userInfo: [NSLocalizedDescriptionKey : description])

NSURL

guard let url = NSURL(string: "https://example.com/image.png") else { return }
let size = CGSize(width: 512, height: 256)
let heightParameterName = "height"
let widthParameterName = "width"

let url2 = url.urlByAppendingAssetSize(size, mode: .Default, heightParameterName: heightParameterName, widthParameterName: widthParameterName)
print(url2.absoluteString)  // on an @2x screen: "https://example.com/image.png?width=1024&height=512"

该方法将 size 乘以 UIScreen.mainScreen().scale 添加到资产 URL,以便资产在屏幕上显示正确的尺寸。

String

let camelCaseStr1 = "userId"
let camelCaseStr2 = "isUserActiveMemberOfCurrentGroup"

print(camelCaseStr1.camelCaseToUnderscore())    // "user_id"
print(camelCaseStr2.camelCaseToUnderscore())    // "is_user_active_member_of_current_group"
"[email protected]".isValidEmailAddress()   // true
"email.example.com".isValidEmailAddress()   // false
let str = "Hello world!"
let range = str.rangeFromString("e", toString: " w")    // Range(1..<7)

UIColor

let red = UIColor(rgb: 0xFF0000)

UIImage

let image = UIImage.imageFromColor(UIColor.redColor(), CGSize(width: 512, height: 256), cornerRadius:4.0)

返回用红色填充、指定大小和指定圆角的 UIImage

let image = UIImage.imageByEmbeddingIconIn(UIImage(named:"profilePhoto"), icon: UIImage(named:"favouriteIcon"))

返回通过在第一张图片上覆盖图标组成的 UIImage

UIView

let view = UIView.viewWithNibNamed("customView")

返回从指定的 nib 创建的视图。

let view = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
view.roundViewCorners(UIRectCorner.AllCorners, radius: 4.0)

UIView 指定角的圆角转换为指定的半径。

然后

let UIView().then {
  $0.backgroundColor = UIColor.blackColor()
}

👥感谢

使用❤️Nodes 制作。

一些功能和调整借鉴自 Hyper 的 Sugar🙈.

📄许可证

CodeMine 基于 MIT 许可协议。更多信息请参阅 LICENSE 文件。