Skip to content

Commit

Permalink
Sending and receiving Codable data with URLSession
Browse files Browse the repository at this point in the history
  • Loading branch information
Tw1stFate committed Jan 18, 2022
1 parent cba0aed commit 2600350
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions SwiftUI-Apprentic/SwiftUI-Apprentic.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
5E553AD62764D54400443127 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5E553AD52764D54400443127 /* Assets.xcassets */; };
5E553AD92764D54400443127 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5E553AD82764D54400443127 /* Preview Assets.xcassets */; };
5E6A53292796F6CC00CAFD8B /* FaceID&TouchIDView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E6A53282796F6CC00CAFD8B /* FaceID&TouchIDView.swift */; };
5E6A532C2796F7A000CAFD8B /* NetworkView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E6A532B2796F7A000CAFD8B /* NetworkView.swift */; };
5EAB840E27871ED80056C827 /* TodoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EAB840D27871ED80056C827 /* TodoView.swift */; };
5EAB8413278723BA0056C827 /* AddTaskView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EAB8412278723BA0056C827 /* AddTaskView.swift */; };
5EAB841627872A3E0056C827 /* Realm in Frameworks */ = {isa = PBXBuildFile; productRef = 5EAB841527872A3E0056C827 /* Realm */; };
Expand All @@ -29,6 +30,7 @@
5E553AD52764D54400443127 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
5E553AD82764D54400443127 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
5E6A53282796F6CC00CAFD8B /* FaceID&TouchIDView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FaceID&TouchIDView.swift"; sourceTree = "<group>"; };
5E6A532B2796F7A000CAFD8B /* NetworkView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkView.swift; sourceTree = "<group>"; };
5EAB840D27871ED80056C827 /* TodoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoView.swift; sourceTree = "<group>"; };
5EAB8412278723BA0056C827 /* AddTaskView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddTaskView.swift; sourceTree = "<group>"; };
5EAB841927872C740056C827 /* RealmManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RealmManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -94,6 +96,14 @@
path = "FaceID&Touch ID";
sourceTree = "<group>";
};
5E6A532A2796F79200CAFD8B /* Network */ = {
isa = PBXGroup;
children = (
5E6A532B2796F7A000CAFD8B /* NetworkView.swift */,
);
path = Network;
sourceTree = "<group>";
};
5EAB840827871A160056C827 /* Controls */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -134,6 +144,7 @@
5EAB840F278721430056C827 /* Demos */ = {
isa = PBXGroup;
children = (
5E6A532A2796F79200CAFD8B /* Network */,
5E6A53272796F69400CAFD8B /* FaceID&Touch ID */,
5EAB840C27871A8E0056C827 /* Todo */,
);
Expand Down Expand Up @@ -220,6 +231,7 @@
5EAB8413278723BA0056C827 /* AddTaskView.swift in Sources */,
5EAB840E27871ED80056C827 /* TodoView.swift in Sources */,
5E553AD42764D54300443127 /* HomePage.swift in Sources */,
5E6A532C2796F7A000CAFD8B /* NetworkView.swift in Sources */,
5EAB842027887FFD0056C827 /* FloatBtn.swift in Sources */,
5E553AD22764D54300443127 /* SwiftUI_ApprenticApp.swift in Sources */,
5EAB841C27886E9D0056C827 /* Task.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// NetworkView.swift
// SwiftUI-Apprentic
//
// Created by zzz on 2022/1/18.
//

import SwiftUI

struct Response: Codable {
var results: [Result]
}

struct Result: Codable {
var trackId: Int
var trackName: String
var collectionName: String
}

struct NetworkView: View {
@State private var results = [Result]()

var body: some View {
List(results, id: \.trackId) { item in
VStack(alignment: .leading) {
Text(item.trackName)
.font(.headline)
Text(item.collectionName)
}
}
.task {
await loadData()
}
}

func loadData() async {
guard let url = URL(string: "https://itunes.apple.com/search?term=taylor+swift&entity=song") else {
print("Invalid URL")
return
}
do {
let (data, _) = try await URLSession.shared.data(from: url)
if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
results = decodedResponse.results
}
} catch {
print("Invalid data")
}
}
}

struct NetworkView_Previews: PreviewProvider {
static var previews: some View {
NetworkView()
}
}

0 comments on commit 2600350

Please sign in to comment.