Skip to content

Commit 99f570f

Browse files
authored
Merge pull request #3813 from allbertoMD/Reto-#21
#21-Swift
2 parents 99dbe84 + f21a280 commit 99f570f

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import Foundation
2+
3+
// Para el correcto funcionamiento del script se debe usar en un playground.
4+
5+
func fetchData(from url: URL, completion: @escaping (Data?, Error?) -> Void) {
6+
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
7+
completion(data, error)
8+
}
9+
task.resume()
10+
}
11+
12+
if let url = URL(string: "https://swift.org") {
13+
fetchData(from: url) { (data, error) in
14+
if let error = error {
15+
print("Error al fetch data: \(error)")
16+
}
17+
guard let dataResponse = data else {
18+
fatalError()
19+
}
20+
if let dataSting = String(data: dataResponse, encoding: .utf8) {
21+
print(dataSting)
22+
}
23+
}
24+
}
25+
26+
27+
28+
29+
// DIFICULTAD EXTRA
30+
print("\nDIFICULTAD EXTRA.")
31+
32+
33+
func order(name order: String, confirmation: @escaping (String) -> Void, done: @escaping (String) -> Void, delivery: @escaping (String) -> Void) {
34+
35+
print("El pedido de \(order) ha sido realizado.")
36+
37+
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
38+
confirmation(order)
39+
}
40+
41+
DispatchQueue.main.asyncAfter(deadline: .now() + 9) {
42+
done(order)
43+
}
44+
45+
DispatchQueue.main.asyncAfter(deadline: .now() + 15) {
46+
delivery(order)
47+
}
48+
}
49+
50+
51+
52+
order(name: "Alitas de pollo") { orderConfirmate in
53+
print("\(orderConfirmate) confirmado.")
54+
} done: { orderDone in
55+
print("\(orderDone) listo.")
56+
} delivery: { orderDelivery in
57+
print("\(orderDelivery) entregado.")
58+
}
59+

0 commit comments

Comments
 (0)