无法转换'String?!'类型的值预期参数类型'通知'

时间:2016-04-12 11:56:43

标签: swift

我试图在使用xcode swift

将数据放入数组之前检查记录的ID

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

  

Notifications.swift:50:46:无法转换'String?'类型的值预期参数类型'通知'

在这一行

*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

}

1 个答案:

答案 0 :(得分:0)

你的一个方法是静态的,另一个不是:

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)
}

由于存在许多可读性问题,因此不确定代码是否会通过编译工作