To run the example project, clone the repo, and run pod install
from the Example directory first.
There is a tests file for all subspec so you could juste see how it work, or continue to read further to the Subspecs section.
- iOS 10.3
- Swift 5
NiceThings is available through CocoaPods. To install all the subspec, simply add the following line to your Podfile:
pod 'NiceThings'
or just add the subspecs you want:
pod 'NiceThings/ArrayRemoveFirstObjectMatching'
This Array extension adds the method removeFirstObject(matching:)
. It removes the first element equal to object
. The element must be Equatable
.
var ages = [12, 16, 2, 7, 16]
ages.removeFirstObject(matching: 16)
print(ages)
// [12, 2, 7, 16]
pod 'NiceThings/CollectionSafeSubscript'
This Array extension provides a subscript [safe:]
returning an optional value for the given index. This is usefull if you don't want to crash with an out of bounds index.
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
pod 'NiceThings/ContionalAssignmentOperator'
This subspec adds the operator variable ??= value
to swift.
It assigns value
to variable
if variable
is nil
; otherwise, variable
stays the same.
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)
pod 'NiceThings/DictionaryMapToDictionary'
This subspec adds the map function to swift dictionary. This function take a closure and return a new dictionary bases on the return of that closure on each (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"]
pod 'NiceThings/OptionalIsNilOrEmpty'
Extension to Collection? to know if it is nil or empty.
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 by philsquared
pod 'NiceThings/Swerl'
Extension to Optional and Result to switch from a type of error handling to another.
unwrap()
:Optional
->throws
unwrap()
:Result
->throws
(with more information)toResult()
:Optional
->Result
toOptional()
:Result
->Optional
assume()
:Result
->fatalError()
I'll be glad to add other usefull swift tricks in this pod, so feel free to make pull request if you wish to see more.
Do not forget the tests ;)
mlemort, maxime.lemort@viseo.com
NiceThings is available under the MIT license. See the LICENSE file for more info.