0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SwiftUI学びの備忘(DispatchQueue.main.async)

Posted at

Swift学習中。GoogleMapのAPIを使うアプリを作りたくて、ひとまず書籍に載ってるWebAPIとJsonを学ぼうと思ったが、うまく行かない部分があり、chatGPTに聞いて解決したことを備忘。

JSONデータのダウンロードまでは無事できたが、必要な項目が配列に入らない。たぶんここが問題だろうと思い、chatGPTに質問。

swift.swift
 for item in items {
                if let name = item.name
                   {
                    let list = ListItem(name: name)
                    DispatchQueue.main.async{
                        self.List.append(list)
                    }
                }
            }            
            print(self.List)

print(self.okashi)の出力結果が[]のまま

GPTの回答
print部分にDispatchQueueを追加

swift.swift
for item in items {
                if let name = item.name
                {
                    let list = ListItem(name: name)
                    DispatchQueue.main.async {
                        self.List.append(list)
                    }
                }
            }
            DispatchQueue.main.async {
                print(self.List)
            }

ひとまず、これで無事に配列に追加されました!

自作アプリ作成までまだ道は遠い

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?