YYSugar 1.0.0

YYSugar 1.0.0

yanyan维护。



YYSugar 1.0.0

YYSugar by Sugar

Sugar是您的Cocoa实现的一个甜味剂。

CI Status Version Carthage Compatible License Platform Swift

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?

轻松访问主Bundle信息。

屏幕

let pixelSize = Screen.pixelSize // CGSize(width: screenWidth * scale, height: screenHeight * scale)

获取设备屏幕的实际像素信息。

模拟器

if !Simulator.isRunning {
  // add device specific operations here
}

当您作为开发者在模拟器中运行应用程序时,可以轻松排除操作,例如不订阅推送通知或运行分析操作等。

键盘观察者

观察键盘显示和隐藏事件,并处理它

let handler = BasicKeyboardHandler()
handler.show = { [weak self] height in
  // move text fields up
}

handler.hide = { [weak self] in
  // move text fields back to original position
}

keyboardObserver = KeyboardObserver(handler: handler)

当前支持

  • BasicKeyboardHandler:基本的 UIView 动画
  • InsetKeyboardHandler:动画 UIScrollView 内边距
  • ConstraintKeyboardHandler:动画底部布局约束
  • CustomKeyboardHandler:自定义处理

iPhone 扩展

UIView

.optimize()
let view = UIView.optimize
/*
  clipsToBounds = true
  layer.drawsAsynchronously = true
  opaque = true
*/

UIImage

+渲染模式
image.original // imageWithRenderingMode(.AlwaysOriginal)
image.template // imageWithRenderingMode(.AlwaysTemplate)

共享

序列类型

let first: Int? = items.findFirst({ $0 > 10 })

日期

比较

if date1 < date2 {
  // do something
} else if date1 >= date2 {
  // do something else
}

构造

let _ = 5.day
let _ = 3.week

框架

let view = UIView()
view.width = 200
view.height = 200
view.x = 25
view.y = 25

print(view.width) // prints 200
print(view.height) // prints 200
print(view.x) // prints 25
print(view.y) // prints 25

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轻松调度。支持所有常规的全局队列:`Main`、`Interactive`、`Initiated`、`Utility`、`Background`。还有`自定义()`用于您的自定义调度队列。

本地化

let string = localizedString("My Profile")
let formattedString = localizedString(key: "%d numbers", arguments: 10)

通过Swift轻松访问`NSLocalizedString`,我们将为您提供更有效的自动完成功能,保证让您满意。

单次

let once = Once()
once.run {
  // do something
}

once.run {
  // no effect
}

操作符

var url = NSURL(string: "hyper.no")!
url ?= NSURL(string: "\\/http")
// url is equal to hyper.no

?=运算符只有在右侧值不是nil时才赋值。

范围

let acceptable = 200..<300
if acceptable.contains(response.statusCode) {
  // Status code is between 200 and 299.
}

正则表达式

if "[email protected]".isEmail() {
  // Is email
}

let stringNumber = "1984"
if stringNumber.isNumber() {
  // Is a number
}

if stringNumber.matches("^[0-9]+$") {
  // Is a number
}

共享扩展

+可排队

struct Object: Queueable {

  func process() -> Bool { return true }
}

let myQueue = [Object(), Object()]
myQueue.processQueue()

轻松创建自己的处理队列,只需使您的对象符合Queueable

public protocol Queueable {
  func process() -> Bool
}

URLStringConvertible

let urlString = "https://hyper.no"
let url = urlString.url

极大地受到/borrowed from Alamofire's 实现 of URLStringConvertible的启发。

核心基础

let string = "hyper/oslo"
string.length // 10
string.truncate(5) // hyper...
string.split(/) // ["hyper", oslo]

if string.isPresent {
  // do something
}

if string.contains("hyper") {
  // found hyper
}

var dirtyString = "   hyper   "
print(dirtyString.trim()) // prints "hyper"

String 上添加了一些额外的功能,如获取长度、截断、修剪或分割字符串。

isPresentisEmpty 的反义词。

contains 可以用来检查字符串是否包含一个单词或短语。

Swizzler

class Swizzled: NSObject {

  override class func initialize() {
    struct Static {
      static var token: dispatch_once_t = 0
    }

    if self !== Swizzled.self {
    return
  }

  dispatch_once(&Static.token) {
    Swizzler.swizzle("method", cls: self)
  }
}

  dynamic func method() -> Bool {
    return true
  }

  func swizzled_method() -> Bool {
    return false
  }
}

let object = Swizzled()
object.method() // false

我们每天都在使用 Swizzler,这曾经是平凡的,现在只是 Swiftling,也就是说,超级快。

Then

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

此实现由 @devxoul 通过他出色的 Then 仓库提供。

类型别名

public typealias JSONArray = [[String : AnyObject]]
public typealias JSONDictionary = [String : AnyObject]

UI测试

if UITesting.isRunning {
  // tests are running
} else {
  // everything is fine, move along
}

易于在运行 UI 测试时包含或排除操作。

单元测试

if UnitTesting.isRunning {
  // running test
}

func testPerformance() {
  let measurement = measure {
    // run operation
  }
}

检查你是否正在运行单元测试以及测量性能。

安装

可以通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile

pod 'YYSugar'

许可

受MIT许可证的保护。有关更多信息,请参阅LICENSE文件。