-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataService.swift
50 lines (37 loc) · 1.07 KB
/
DataService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// DataService.swift
// RecipeList
//
// Created by Nick Jüttner on 19.03.23.
//
import Foundation
class DataService {
static func getLocalData() -> [Recipe] {
let pathString = Bundle.main.path(forResource: "recipes", ofType: "json")
guard pathString != nil else {
return [Recipe]()
}
let url = URL(fileURLWithPath: pathString!)
do {
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
do {
let recipeData = try decoder.decode([Recipe].self, from: data)
for r in recipeData {
r.id = UUID()
for i in r.ingredients {
i.id = UUID()
}
}
return recipeData
}
catch {
print(error)
}
}
catch {
print(error)
}
return [Recipe]()
}
}