Skip to content

Instantly share code, notes, and snippets.

View jdev7's full-sized avatar

Juan Navas jdev7

View GitHub Profile
@jdev7
jdev7 / ReduceStringsCollection.swift
Created November 1, 2017 19:35
Reduce strings collection
let stringsCollection = ["Swi", "ft ", "ro", "cks", "!!"]
let finalString = stringsCollection.reduce("", +)
// Swift rocks!!
@jdev7
jdev7 / ReduceCollection.swift
Created November 1, 2017 19:35
Reduce collection
let collection = [5, 3, 10, 1, 7]
let total = collection.reduce(0, +)
// 26
@jdev7
jdev7 / ReduceBooleans.swift
Created November 1, 2017 20:36
Reduce Booleans
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
@jdev7
jdev7 / ReducePersons.swift
Created November 1, 2017 21:00
Reducing over objects
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
@jdev7
jdev7 / ReduceRandomString.swift
Created November 1, 2017 21:46
Using reduce to get a random String
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]))
})
}
@jdev7
jdev7 / ReduceRandomStringFunction.swift
Created November 1, 2017 21:52
Using reduce to get a random string, with a function
extension String {
static func randomString(size: UInt) -> String {
let stringSet = "0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"
return (0..<size).reduce("", stringSet.randomCharacter)
}
private func randomCharacter(partialResult: String, iterationIndex: UInt) -> String {
let randomIndex = arc4random_uniform(UInt32(count))
let charIndex = index(startIndex, offsetBy: String.IndexDistance(randomIndex))
return partialResult.appending(String(self[charIndex]))
@jdev7
jdev7 / ReduceRandomStringOptimized.swift
Created November 3, 2017 16:59
Random string by reducing, optimized
extension String {
static func randomStringOptimized(size: UInt) -> String {
let stringSet = "0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"
return (0..<size).reduce(into: "", stringSet.randomCharacterOptimized)
}
private func randomCharacterOptimized(partialResult: inout String, iterationIndex: UInt) {
let randomIndex = arc4random_uniform(UInt32(count))
let charIndex = index(startIndex, offsetBy: String.IndexDistance(randomIndex))
@jdev7
jdev7 / MeasuringImprovementReduce1000iterations.swift
Last active November 3, 2017 17:04
Measuring improvement against unoptimized reduce
Total time: 2.64654844999313
TotalTimeOptimized: 2.50773054361343
Total time difference: 0.1388179063797
Average: 0.0001388179063797
@jdev7
jdev7 / IntrinsicContentSizeTableView.swift
Last active May 25, 2020 14:23
A UITableView subclass that adapts to its content size, up to a seteable maxHeight
// Idea obtained from https://medium.com/@dushyant_db/swift-4-recipe-self-sizing-table-view-2635ac3df8ab
// it can be useful if you want to center it in the screen (let's say in an iPad for example), or take up all the available
// height. Also it could be used to be placed inside a UIStackView
class IntrinsicContentSizeTableView: UITableView {
var maxHeight: CGFloat = UIScreen.main.bounds.size.height
override var intrinsicContentSize: CGSize {
let height = min(contentSize.height, maxHeight)
return CGSize(width: contentSize.width, height: height)
}
@jdev7
jdev7 / gist:ac12402ec4d331d723de55c6e48635ec
Created July 1, 2020 10:28
Command to launch an URL in the iOS simulator, which is useful to try Universal Links (since in Safari you can't launch the app with a Universal Link)
xcrun simctl openurl booted https://www.example.com/faq
// https://objectivetidbits.com/2017/06/08/working-with-universal-links-on-ios-simulator/