NiceThings 1.4.1

NiceThings 1.4.1

Maxime LEMORT维护。



  • mlemort

NiceThings

CI Status Version License Platform

示例

要运行示例项目,首先克隆仓库,然后从Example目录运行pod install

Tests文件夹下有所有subspects的测试文件,因此您可以查看其工作原理,或者继续阅读关于Subspecs的部分。

需求

  • iOS 10.3
  • macOS 10.15
  • tvOS 13.3
  • Swift 5

安装

NiceThings可以通过CocoaPods使用。要安装所有subspects,只需将以下行添加到您的Podfile中

pod 'NiceThings'

或者只添加您想要的subspects,见下文。

Subspecs

ArrayRemoveFirstObjectMatching

pod 'NiceThings/ArrayRemoveFirstObjectMatching'

此数组扩展添加了方法 removeFirstObject(matching:)。它移除等于 object 的第一个元素。该元素必须是 Equatable

var ages = [12, 16, 2, 7, 16]
ages.removeFirstObject(matching: 16)
print(ages)
// [12, 2, 7, 16]

CollectionSafeSubscript

pod 'NiceThings/CollectionSafeSubscript'

此数组扩展提供了下标 [safe:],返回给定索引的可选值。如果您不想因越界索引而崩溃,这很有用。

var ages = [12, 16, 2, 7, 16]
if ages[safe: 2] != nil {
    print("ages has at least 3 entries")
}
if ages[safe: 25] == nil {
    print("ages has less then 26 entries")
}
// ages has at least 3 entries
// ages has less then 26 entries

ContionalAssignmentOperator

pod 'NiceThings/ContionalAssignmentOperator'

此子规范向 Swift 添加了运算符 variable ??= value。如果变量 variable 是 nil,则将 value 分配给 variable;否则,variable 保持不变。

var val1: Int?
val1 ??= 12
print(val1 as Any)
//Optional(12)

var val2: String? = "Foo"
val2 ??= "Bar"
print(val2 as Any)
//Optional("Foo")

var val3: Double? = 3.14
val3 ??= nil
print(val3 as Any)
//Optional(3.1400000000000001)

var val4: Int?
val4 ??= 1 ??= 2 ??= 3 ??= 4 ??= nil
print(val4 as Any)
//Optional(1)

DictionaryMapToDictionary

pod 'NiceThings/DictionaryMapToDictionary'

此子规范向 Swift 字典添加了映射方法。它以闭包作为参数,并将该闭包应用于每个 (key, value) 对以生成新的字典。

let dic = ["A": "a", "B": "b", "C": "c"]
let newDic = dic.map {
    return ($0.lowercased(), $1.uppercased())
}
print(newDic)
// ["c": "C", "a": "A", "b": "B"]

OptionalIsNilOrEmpty

pod 'NiceThings/OptionalIsNilOrEmpty'

对 Collection? 进行扩展以判断其是否为 nil 或空。

let str1: String? = "not a nil or empty string"
print(str1.isNilOrEmpty)
//false
let str2: String? = ""
print(str2.isNilOrEmpty)
//true
let str3: String? = nil
print(str3.isNilOrEmpty)
//true

Swerl 由philsquared编写

pod 'NiceThings/Swerl'

从一种错误处理类型切换到另一种类型的可选性和结果的扩展。

  • unwrap() : Optionalthrows
  • unwrap() : Resultthrows(带更多信息)
  • toResult() : OptionalResult
  • toOptional() : ResultOptional
  • assume() : ResultfatalError()
    Swerl

协作

我很乐意在这个pod中添加其他有用的Swift技巧,如果您想获得更多功能,请随时提交拉取请求
别忘了测试;)

作者

mlemort,[email protected]

许可

NiceThings 在MIT许可下可用。有关更多信息,请参阅LICENSE文件。