You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that the Swift Package Manager is still in early design and development, but SwiftlyExt does support its use on supported platforms.
Usage
There are many handy usages of SwiftlyExt, head over CocoaDocs for the comprehensive documentation.
We'll try to list some of the cool examples here.
Array Extensions
[π,π€‘,β€οΈ,π].sample() // Return a random element
// => π
[π,π€‘,β€οΈ,π].sampleSize(2) // Return n random elements
// => [π€‘, π]
[1,2,3,4,5].dropWhile{ $0 <3} //Drop elements that passes the predicate from the beginning to end
// => [3, 4, 5]
[1,2,3,4,5].dropWhile{ $0 <3}.some{$0 %2==0} //And YES you can use method chaining too π
// => true
[0,11,28,10].every{ $0 %2==0} //Check if all elements in the array passed the condition
// => false
[0,11,28,10].some{ $0 %2!=0} //Check if one of the element passes the condition
// => true
[1,2,3,4,5].findLastIndex{$0 %2==0} //Find index of the last number which predicate return true for.
// => 3
[1,2,3,4,5].groupBy{ $0 %2==0?"even":"odd"} //Group common elements from an array to a dictionary of [Hashable : [Element]]
// => ["even": [2,4], "odd": [1,3,5]]
// Any many more....
Date Extensions
letnow=Date()lettmr= now.date(byAddingDays:1).date(byAddingMinutes:20) // You could also add year, month... and other time units
now.isBefore(tmr)
// => true
now?.toString(format:"dd/MM/yyyy HH:mm:ss") // Return the string representation for a date.
// => "03/15/2017 14:34:22"
tmr.year ==2017 // Access time unit properties
tmr.hour ==14
tmr.minute ==54
String Extensions
"John Doe".initials // Return the initials of the String
// => "JD"
"swift@swiftly.com".isEmail // Email validation
// => true
"<p>π―</p>".between("<p>","</p>") // Find the string between two string
// => "π―"
"01/01/1970 00:34:22".date(format:"dd/MM/yyyy HH:mm:ss") // Return a date from current string
// => Date("01/01/1970 00:34:22")
"https://github.com/Swiftly".base64Encoded // Return base64encoded string
// => "aHR0cHM6Ly9naXRodWIuY29tL1N3aWZ0bHk="
"\n\n\n Swiftly ".trimmed.reversed // Trim newline and spaces and reverse the string
// => "yltfiwS"
"Swiftly\t\nString\nTest".urlEncoded // URL Encoded
// => "Swiftly%09%0AString%0ATest"
"https%3A%2F%2Fgithub.com%2Fkhoiln%2FSwiftlyEXT".urlDecoded // URL Decoded
// => "https://github.com/khoiln/SwiftlyEXT"
// Any many more....
Int Extensions
1.upTo(3){print($0)}
// print 1, 2, 3
5.times{print("πΆ")} // Run a block n times
// print πΆ 5 times
1234.digits() // Convert integer to array of digits
// => [1,2,3,4]
201.isIn(range:200..<300) // Test whether a int is in a range
// => true
// And many more
How to contribute
Any help or feedback is highly appreciated. Please refer to the contributing guidelines for more information.
License
SwiftyExt is released under the MIT license. See LICENSE for details.
About
SwiftlyExt is a collection of useful extensions for Swift 3 standard classes and types π