我有一个博客,其中的条目具有两个属性。 “ eventDate”和“ title”。
我有以下代码下载“ eventDate”:

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {

    if elementName == "item" {
        currentEvent = EventPosts()

        return
    }

    if elementName == "eventDate" {
        parsedElement = "eventDates"
    }

    if elementName == "title" {
        parsedElement = "title"
    }

}

func parser(parser: NSXMLParser, foundCharacters string: String) {

    let str = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

    if parsedElement == "eventDates" && str != "" {
        if currentEvent.eventDates.isEmpty {

            currentEvent.eventDates = str
            parsedElement = ""

            let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            let managedContext = appDelegate.managedObjectContext
            let entity =  NSEntityDescription.entityForName("Calendar", inManagedObjectContext:managedContext)
            let datum = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
            datum.setValue(currentEvent.eventDates, forKey: "date")

            do {
                try managedContext.save()
                dates.append(datum)
            } catch let error as NSError  {
                print("Could not save \(error), \(error.userInfo)")
            }
        }

    }
}


现在,我想将匹配的“标题”保存到名为Calendar的匹配核心数据实体中。

这段代码是我尝试过的,但是我猜它不会创建匹配对。它仅创建带有eventDates的Calendar条目和带有标题的Calendar条目,而不同时创建带有匹配对的Calendar条目...

    if parsedElement == "eventDates" && str != "" {
        if currentEvent.eventDates.isEmpty {

            currentEvent.eventDates = str
            parsedElement = ""

            let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            let managedContext = appDelegate.managedObjectContext
            let entity =  NSEntityDescription.entityForName("Calendar", inManagedObjectContext:managedContext)
            let datum = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
            datum.setValue(currentEvent.eventDates, forKey: "date")

            do {
                try managedContext.save()
                dates.append(datum)
            } catch let error as NSError  {
                print("Could not save \(error), \(error.userInfo)")
            }
        }

    }

    if parsedElement == "title" && str != "" {
        if currentEvent.eventDates.isEmpty {

            currentEvent.eventDates = str
            parsedElement = ""

            let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            let managedContext = appDelegate.managedObjectContext
            let entity =  NSEntityDescription.entityForName("Calendar", inManagedObjectContext:managedContext)
            let title = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
            title.setValue(currentEvent.title, forKey: "title")

            do {
                try managedContext.save()
                dates.append(title)
            } catch let error as NSError  {
                print("Could not save \(error), \(error.userInfo)")
            }
        }

    }


用“ title”和“ eventDate”匹配对创建Calendar实体的正确代码是什么?非常感谢您的帮助。

编辑到目前为止已经尝试过:

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {

    if elementName == "item" {
        currentEvent = EventPosts()

        return
    }

    if elementName == "eventDate" {
        parsedElement = "eventDates"
        print(currentEvent.eventDates)
    }

    if elementName == "title" {
        parsedElement = "title"
        print(currentEvent.title)
    }

    if !currentEvent.eventDates.isEmpty && !currentEvent.title.isEmpty {

        parsedElement = ""

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext
        let entity =  NSEntityDescription.entityForName("Calendar", inManagedObjectContext:managedContext)
        let datum = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)


        datum.setValue(currentEvent.eventDates, forKey: "date")
        datum.setValue(currentEvent.title, forKey: "title")

        do {
            try managedContext.save()
            dates.append(datum)
        } catch let error as NSError  {
            print("Could not save \(error), \(error.userInfo)")
        }
    } else if currentEvent.eventDates.isEmpty && !currentEvent.title.isEmpty {
        print("currentEvent.eventDates.isEmpty")
    } else if !currentEvent.eventDates.isEmpty && currentEvent.title.isEmpty {
        print("currentEvent.title.isEmpty")
    } else {
        print("both.isEmpty")
    }
}


控制台用此代码说both.isEmpty百万次。但是请记住:在我的第一个代码中,我将eventDates完美保存了!

最佳答案

以下假设您仅想在两个信息均可用的情况下保存对象。这个想法是将托管对象的创建移动到解析之后。

    if parsedElement == "eventDates" && str != "" {
        if currentEvent.eventDates.isEmpty {
            currentEvent.eventDates = str
            parsedElement = ""
        }
    }

    if parsedElement == "title" && str != "" {
        if currentEvent.title.isEmpty {
            currentEvent.title = str
            parsedElement = ""
        }
    }

    if !currentEvent.eventDates.isEmpty && !currentEvent.title.isEmpty {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext
        let entity =  NSEntityDescription.entityForName("Calendar", inManagedObjectContext:managedContext)
        let datum = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
        datum.setValue(currentEvent.eventDates, forKey: "date")
        datum.setValue(currentEvent.title, forKey: "title")

        do {
            try managedContext.save()
            dates.append(datum)
        } catch let error as NSError  {
            print("Could not save \(error), \(error.userInfo)")
        }
    }

10-08 02:20