Skip to content

#21-Swift #3813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Roadmap/21 - CALLBACKS/swift/allbertoMD.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import Foundation

// Para el correcto funcionamiento del script se debe usar en un playground.

func fetchData(from url: URL, completion: @escaping (Data?, Error?) -> Void) {
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
completion(data, error)
}
task.resume()
}

if let url = URL(string: "https://swift.org") {
fetchData(from: url) { (data, error) in
if let error = error {
print("Error al fetch data: \(error)")
}
guard let dataResponse = data else {
fatalError()
}
if let dataSting = String(data: dataResponse, encoding: .utf8) {
print(dataSting)
}
}
}




// DIFICULTAD EXTRA
print("\nDIFICULTAD EXTRA.")


func order(name order: String, confirmation: @escaping (String) -> Void, done: @escaping (String) -> Void, delivery: @escaping (String) -> Void) {

print("El pedido de \(order) ha sido realizado.")

DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
confirmation(order)
}

DispatchQueue.main.asyncAfter(deadline: .now() + 9) {
done(order)
}

DispatchQueue.main.asyncAfter(deadline: .now() + 15) {
delivery(order)
}
}



order(name: "Alitas de pollo") { orderConfirmate in
print("\(orderConfirmate) confirmado.")
} done: { orderDone in
print("\(orderDone) listo.")
} delivery: { orderDelivery in
print("\(orderDelivery) entregado.")
}