gpt4 book ai didi

swift - 无法将类型 'String?!' 的值转换为预期参数类型 'Notifications'

转载 作者:行者123 更新时间:2023-11-30 13:31:09 24 4
gpt4 key购买 nike

在将记录放入数组之前,我尝试使用 xcode swift 检查记录的 id

这是代码。但是,我收到以下错误

Notifications.swift:50:46: Cannot convert value of type 'String?!' to expected argument type 'Notifications'

在这条线上

*if (readRecordCoreData(result["MessageID"])==false)*

请有人帮忙解释一下这个错误

import CoreData

struct Notifications{

var NotifyID = [NSManagedObject]()

let MessageDesc: String
let Messageid: String


init(MessageDesc: String, Messageid:String) {

self.MessageDesc = MessageDesc
self.Messageid = Messageid
// self.MessageDate = MessageDate



}

static func MessagesWithJSON(results: NSArray) -> [Notifications] {
// Create an empty array of Albums to append to from this list

var Notification = [Notifications]()

// Store the results in our table data array
if results.count>0 {

for result in results {

//get fields from json

let Messageid = result["MessageID"] as! String
let MessageDesc = result["MessageDesc"] as? String

let newMessages = Notifications(MessageDesc: MessageDesc!, Messageid:Messageid)



//check with id's from core data
if (readRecordCoreData(result["MessageID"])==false)
{

Notification.append(newMessages)
}



}
}
return Notification
}

//check id
func readRecordCoreData(Jsonid: String) -> Bool {

var idStaus = false

let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate

let managedContext = appDelegate.managedObjectContext

//2
let fetchRequest = NSFetchRequest(entityName: "ItemLog")


//3
do {

let resultsCD = try! managedContext.executeFetchRequest(fetchRequest)



if (resultsCD.count > 0) {

for i in 0 ..< resultsCD.count {
let match = resultsCD[i] as! NSManagedObject
let id = match.valueForKey("notificationID") as! String

if (Jsonid as String! == id)
{
idStaus = true

}
else{

idStaus = false

}

}
}

} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}

return idStaus

}

最佳答案

您的方法之一是静态的,而另一个则不是:

func readRecordCoreData(Jsonid: String) -> Bool 
static func MessagesWithJSON(results: NSArray) -> [Notifications]

根据您想要完成的任务,您可以声明静态、无或替换

 //check with id's from core data
if (readRecordCoreData(result["MessageID"])==false)
{
Notification.append(newMessages)
}

 //check with id's from core data
if (Notifications.readRecordCoreData(Messageid)==false)
{
Notification.append(newMessages)
}

不确定代码在编译后是否可以工作,但是存在许多可读性问题

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

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