This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
static func randomString(size: UInt) -> String { | |
let stringSet = "0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ" | |
return (0..<size).reduce("", { (partialResult, _) -> String in | |
let randomIndex = arc4random_uniform(UInt32(stringSet.count)) | |
let charIndex = stringSet.index(stringSet.startIndex, offsetBy: String.IndexDistance(randomIndex)) | |
return partialResult.appending(String(stringSet[charIndex])) | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Person { | |
let name: String | |
let lastName: String | |
let birthDate: Date | |
init(name: String, lastName: String, birthDate: String) { | |
let df = DateFormatter() | |
df.dateFormat = "dd-MM-yyyy" | |
self.name = name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let booleans = [true, true, false, true, false] | |
let booleans2 = [true, true, true, true, true] | |
let result = booleans.reduce(true, { $0 && $1 }) | |
let result2 = booleans2.reduce(true, { $0 && $1 }) | |
print(result) // false | |
print(result2) // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let collection = [5, 3, 10, 1, 7] | |
let total = collection.reduce(0, +) | |
// 26 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let stringsCollection = ["Swi", "ft ", "ro", "cks", "!!"] | |
let finalString = stringsCollection.reduce("", +) | |
// Swift rocks!! |
NewerOlder