我的博客中包含两个属性的条目。 “eventDate”& “标题”。 我有以下代码来下载“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条目的日历条目,但不包含两个和匹配的对...
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!
答案 0 :(得分:1)
以下假设您只想在两条信息都可用时保存对象。我们的想法是将托管对象的创建移动到解析之后。
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)")
}
}