SwiftExtensions 0.0.2

SwiftExtensions 0.0.2

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2015年6月
SPM支持 SPM

anatoliyv 维护。



  • 作者
  • Anatoliy Voropay

Swift 扩展

适用于不同类的一些有用的 Swift 扩展。创建此仓库的主要原因是我在大多数项目中都需要它,因此决定与其它开发者分享。

信息: 扩展集合正在更新。如果您想向此仓库添加一些有用的功能,请给我发消息。

Foundation 框架

NSDate

func isTheSameDay(date: NSDate) -> Bool

用来检查给定日期是否与 NSDate 相同。

var dateBefore = NSDate(timeIntervalSinceNow: -3600 * 24 - 10)
var dateAfter = NSDate(timeIntervalSinceNow: 3600 * 24 + 10)

NSDate().isTheSameDay(NSDate()) // is true
dateBefore.isTheSameDay(NSDate()) // is false
dateAfter.isTheSameDay(NSDate()) // is false

class func hourlyTimingString(timeInterval: NSTimeInterval?) -> String

用来生成可读的时间间隔字符串。例如,超过 1 小时的间隔为 “23:12:24”,不足 1 小时的间隔为 “23:12”。

NSDate.hourlyTimingString(12) // will return '00:12'
NSDate.hourlyTimingString(3600 - 1) // will return '59:59'
NSDate.hourlyTimingString(3600 + 1) // will return '1:00:01'
NSDate.hourlyTimingString(3600 * 67 + 1) // will return '67:00:01'

class func readableTimeInterval(date: NSDate) -> String

用来生成可读的时间间隔字符串。例如,为 “3 分钟前” 或 “2 天前”。

NSDate.hourlyTimingString(NSDate(timeIntervalSinceNow:3600 - 1)) // 59 minutes ago
NSDate.readableTimeInterval(NSDate(timeIntervalSinceNow:-3600 - 10)) //  1 hour ago
NSDate.readableTimeInterval(NSDate(timeIntervalSinceNow:-3600 * 24 * 5 - 10)) // 5 days ago
NSDate.readableTimeInterval(NSDate(timeIntervalSinceNow:-3600 * 24 * 31 * 5 - 10)) // 5 months ago

class func readableDate(date: NSDate) -> String

用来获取过去日期的易读格式。对于小于 24 小时的时间将返回具体的 hour:minute,对于小于 1 周的时间将返回 星期几。其它日期将使用 en_US_POSIX 格式。

NSObject

func delay(delay: Double, closure:()->()) 在延迟后执行闭包。闭包将在主线程中调用

NSDate.delay(5, closure: {
    println("After 5 seconds")
})

NSString

class func isEmailValid(email: String) -> Bool

如果电子邮件有效,则返回 true。此验证仅检查字符串是否匹配正则表达式。它不会检查域名扩展。也不能保证它是一个真正的电子邮件地址。

NSString.isEmailValid("not [email protected]") // false
NSString.isEmailValid("[email protected]") // false
NSString.isEmailValid("notvalid@gmail.") // false

NSString.isEmailValid("[email protected]") // true
NSString.isEmailValid("[email protected]") // true

NSFileManager

func fileSizeAtPath(path: String) -> Int

将返回给定路径下文件的大小。如果文件不存在,则返回 0。

UIKit 框架

UIColor

class func colorWithHex(hex: String) -> UIColor

此函数将根据十六进制字符串创建UIColor。无论十六进制字符串是否包含#前缀,都能正确工作。如果不能将字符串转换为UIColor(例如,颜色字符串长度小于6个符号时),将返回UIColor.clearColor()。在返回的UIColor中,alpha通道始终为1

UIColor.colorWithHex("#FFAA66")

UIView

var width: CGFloat // Returns frames width
var height: CGFloat // Returns frames height
var x: CGFloat // Returns frames origin x
var y: CGFloat // Returns frames origin y
var centerX: CGFloat // Returns frames center x
var centerY: CGFloat // Returns frames center y
func setWidth(width: CGFloat) // Update frames width
func setHeight(height: CGFloat) // Returns frames height
func setX(x: CGFloat) // Updates frames origin x position
func setY(y: CGFloat) // Updates frames origin y position
func setCenterX(x: CGFloat) // Updates frames center x position
func setCenterY(y: CGFloat)  // Updates frames center y position

Swift

数组

mutating func removeObject(object: U)

从这个数组中移除对象。如果数组中有多个相同的对象,也将移除。

func contains(object: T) -> Bool

如果数组包含对象,将返回true。对象需要遵守Equatable协议。

字符串

func length() -> Int // Returns length of a string
func trim() -> String // Returns trimmed string
func substring(location: Int, length: Int) -> String! // Returns substring  with `length` of a string from `location`
func location(substring: String) -> Int // Returns substring location or NSNotFound
func contains(substring: String) -> Bool // Returns `true` if string contains substring
func isNumeric() -> Bool // Returns `true` if string is numeric

subscript(index: Int) -> String!

返回指定索引处的字符。例如

"abc"[1] == "b"
"abs"[-1] == "s"

整数

var isEven: Bool // Returns true if number is even
var isOdd: Bool // Returns true if number is odd
var isPositive: Bool // Returns true if number is positive
var isNegative: Bool // Returns true if number is negative
var isZero: Bool // Returns true if number is zero
var isPOZ: Bool // Returns true if number is positive or zero
func double() -> Double // Casts to double
func float() -> Float // Casts to float

func times(closure: () -> ())

此方法将重复执行closure n次。可能的用法

12.times { 
    println("Hello worlds") 
}