我有一个Artist
类,下面是一个创建新artist
并将其插入我的库数组的函数.因为insertToArtists(artist:)
是一个异步函数,所以我必须把它包装成Task
.但我也希望我的newArtist
返回刚刚创建的artist
.
我的代码如下所示,但Xcode告诉我一个错误:No 'init' candidates produce the expected contextual result type 'Artist'
func newArtist(name: String, notes: String, artwork: NSImage?) -> Artist {
Task {
let artist = Artist(name: name, notes: notes, artwork: artwork)
await insertToArtists(artist: artist)
return artist
}
}
但如果我的代码如下所示.它不会正常工作.它似乎在insertToArtist(artist:)
之前返回artist
.插入后如何返回artist
.谢谢你的帮助.
func newArtist(name: String, notes: String, artwork: NSImage?) -> Artist {
let artist = Artist(name: name, notes: notes, artwork: artwork)
Task {
await insertToArtists(artist: artist)
}
return artist
}