Last active
March 26, 2024 13:38
-
-
Save benigumocom/ee3dd8a37f752847fa65c4842491b315 to your computer and use it in GitHub Desktop.
【SwiftUI】task の id を使って処理を何度も繰り返す 👉 https://android.benigumo.com/20240326/task-id/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct TestTaskID: View { | |
| @State private var text = "" | |
| @State private var taskID = UUID() | |
| var body: some View { | |
| Text("\(text)") | |
| .task(id: taskID) { // * | |
| let url = URL(string: "https://worldtimeapi.org/api/timezone/Asia/Tokyo.txt")! | |
| do { | |
| var lines: [String] = [] | |
| for try await line in url.lines { | |
| lines.append(line) | |
| } | |
| text = String(lines[2].components(separatedBy: "T")[1]) | |
| } catch { | |
| text = "Faild to load." | |
| } | |
| } | |
| Button("RELOAD") { | |
| taskID = UUID() | |
| } | |
| .buttonStyle(.borderedProminent) | |
| } | |
| } | |
| #Preview { | |
| TestTaskID() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment