gpt4 book ai didi

swift - 无法将类型 '[Message]' 的值转换为预期的参数类型 'Message'

转载 作者:行者123 更新时间:2023-11-28 12:06:01 27 4
gpt4 key购买 nike

我正在关注 this教程,我收到标题中显示的错误,这是我的代码:

func loadData()
{
let delegate = UIApplication.shared.delegate as? AppDelegate

if let context = delegate?.persistentContainer.viewContext
{
if let friends = fetchFriends()
{
messages = [Message]()

for friend in friends
{
print(friend.name) // This line btw for some reason is giving me: Expression implicitly coerced from 'String?' to Any

let fetchRequest:NSFetchRequest<Message> = Message.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]
fetchRequest.predicate = NSPredicate(format: "friend.name = %@", friend.name!)
fetchRequest.fetchLimit = 1

do
{
let fetchedMessages = try(context.fetch(fetchRequest))
messages?.append(fetchedMessages)
// This above line is the line which is giving me this error:
//Cannot convert value of type '[Message]' to expected argument type 'Message'
}
catch let err
{
print(err)
}
}
}
}
}

正如您从大约 13 分钟看到的那样,我的代码看起来并不完全相同,因为视频有点旧并且使用的是旧版本的 swift。

我希望有人能帮我解决这个错误,因为我不知道该怎么做,提前谢谢你。

最佳答案

尝试下行

messages.append(contentsOf: fetchedMessages) 

代替:messages.append(fetchedMessages)

关于swift - 无法将类型 '[Message]' 的值转换为预期的参数类型 'Message',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49361096/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com