You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+98-1Lines changed: 98 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -178,7 +178,104 @@ NavigationView {
178
178
179
179

180
180
181
-
### Uploading to the App Store
181
+
### Networking
182
+
183
+
1. Let's create a new screen that displays a list of items from an API. Right click on the folder tree and click "New File...". Select "Swift File" and click "Next". Enter "NetworkingView.swift" as the file name and click "Create". You should see a new file called `NetworkingView.swift` in the folder tree.
184
+
185
+
2. In `NetworkingView.swift`, let's call the [JSON Placeholder API](https://jsonplaceholder.typicode.com/) to get a list of posts. Add the following shown below. By creating a `@State var posts: [Post]` property, we can store the list of posts in the view. You can read more about state [here](<https://developer.apple.com/documentation/swiftui/state>).
186
+
187
+
```swift
188
+
importSwiftUI
189
+
190
+
structNetworkingView: View {
191
+
@Statevar posts: [Post] = []
192
+
193
+
var body: some View {
194
+
List(posts, id: \.id) { post in
195
+
VStack(alignment: .leading) {
196
+
Text(post.title)
197
+
.font(.headline)
198
+
Text(post.body)
199
+
.font(.subheadline)
200
+
}
201
+
}
202
+
.task {
203
+
do {
204
+
posts =tryawaitfetchPosts()
205
+
} catch {
206
+
print(error)
207
+
}
208
+
}
209
+
}
210
+
211
+
funcfetchPosts() asyncthrows-> [Post] {
212
+
let url =URL(string: "https://jsonplaceholder.typicode.com/posts")!
213
+
let (data, _) =tryawait URLSession.shared.data(from: url)
3. A task is a piece of code that runs asynchronously. You can read more about tasks [here](https://docs.swift.org/swift-book/LanguageGuide/Concurrency/ConcurrencyQuickTour.html#ID617). In the `task` modifier, we call the `fetchPosts` function to get a list of posts. We then set the `posts` property to the list of posts. You can read more about the `task` modifier [here](https://developer.apple.com/documentation/swiftui/view/task(id:priority:)).
226
+
227
+
4. A `Codable` type is a type that can be encoded and decoded from JSON. You can read more about it [here](https://developer.apple.com/documentation/swift/codable). We create a `Post` struct that conforms to the `Codable` protocol. We also make it conform to the `Identifiable` protocol so that we can use it in a `List`. You can read more about the `Identifiable` protocol [here](https://developer.apple.com/documentation/swiftui/identifiable).
228
+
229
+
5. An `Identifiable` type is a type that has a stable identity. You can read more about it [here](https://developer.apple.com/documentation/swiftui/identifiable). We make the `id` property of the `Post` struct the stable identity.
230
+
231
+
6. Go back to `ContentView.swift`. Add a navigation link to the `VStack`, under `NavigationLink`. You can read more about it [here](https://developer.apple.com/documentation/swiftui/navigationlink).
1. Now that we've gotten some basic SwiftUI down, let's style the app. Go back to `ContentView.swift`. Add the following code to the `VStack`, under `NavigationLink`. You can read more about the `background` modifier [here](<https://developer.apple.com/documentation/swiftui/view/background(_:alignment:)>).
0 commit comments