Ios Swift中的核心数据:仅保存for循环中的最后一个对象
我试图将核心数据中的多个对象保存到Ios Swift中的核心数据:仅保存for循环中的最后一个对象,ios,swift,for-loop,core-data,Ios,Swift,For Loop,Core Data,我试图将核心数据中的多个对象保存到for循环中的IPodSongs实体,即当前在for song in result{}循环中的歌曲标题。但是我的代码只保存循环中的最后一首歌,并且一直覆盖同一个对象。每次我都需要创建一个新对象,而不是覆盖同一个对象。我做错了什么 func fetchiPodSongsOnSignup() { var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppD
for
循环中的IPodSongs实体,即当前在for song in result{}
循环中的歌曲标题。但是我的代码只保存循环中的最后一首歌,并且一直覆盖同一个对象。每次我都需要创建一个新对象,而不是覆盖同一个对象。我做错了什么
func fetchiPodSongsOnSignup() {
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = appDel.managedObjectContext!
var newSong = NSEntityDescription.insertNewObjectForEntityForName("IPodSongs", inManagedObjectContext: context) as! NSManagedObject
var request = NSFetchRequest(entityName: "IPodSongs")
request.returnsObjectsAsFaults = false
var results = context.executeFetchRequest(request, error: nil)
let query = MPMediaQuery.songsQuery()
let result = query.collections as! [MPMediaItemCollection]
for song in result {
for song in song.items as! [MPMediaItem] {
newSong.setValue("\(song.valueForProperty(MPMediaItemPropertyTitle))", forKey: "title")
println(newSong)
context.save(nil)
}
}
在所有迭代中使用相同的插入对象:
for song in song.items as! [MPMediaItem] {
newSong.setValue("\(song.valueForProperty(MPMediaItemPropertyTitle))", forKey: "title")
您确实在更改歌曲的对象值,但您使用的是同一实例,而该实例又是核心数据库中的同一实体
您需要为每首新歌使用一个新实例:
var newSong = NSEntityDescription.insertNewObjectForEntityForName("IPodSongs", inManagedObjectContext: context) as! NSManagedObject
newSong.setValue("\(song.valueForProperty(MPMediaItemPropertyTitle))", forKey: "title")
您不需要创建将新对象保存到核心数据的请求。您所做的错误是创建单个托管对象,插入它,然后通过循环对其进行更改,而不是为每首歌曲创建一个新对象 这应该有效:
func fetchiPodSongsOnSignup() {
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = appDel.managedObjectContext!
let query = MPMediaQuery.songsQuery()
let result = query.collections as! [MPMediaItemCollection]
for song in result {
for song in song.items as! [MPMediaItem] {
var newSong = NSEntityDescription.insertNewObjectForEntityForName("IPodSongs", inManagedObjectContext: context) as NSManagedObject
newSong.setValue("\(song.valueForProperty(MPMediaItemPropertyTitle))", forKey: "title")
println(newSong)
}
}
if context.save(&errorPointer == false {
printlin("Error received while saving")
}
}
如果context.save(&errorPointer==false{printlin(“保存时收到错误”)},也将
放在最末尾。试试这个,这会起作用
插入for循环并保存到外部循环
func fetchiPodSongsOnSignup() {
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext = appDel.managedObjectContext!
let query = MPMediaQuery.songsQuery()
let result = query.collections as! [MPMediaItemCollection]
for song in result {
for song in song.items as! [MPMediaItem] {
var newSong = NSEntityDescription.insertNewObjectForEntityForName("IPodSongs", inManagedObjectContext: context) as NSManagedObject
newSong.setValue("\(song.valueForProperty(MPMediaItemPropertyTitle))", forKey: "title")
context.insert(newSong)
}
}
context.save(nil)
}
这很可能对你有用!试试看
for song in result {
for song in song.items as! [MPMediaItem] {
var newSong = NSEntityDescription.insertNewObjectForEntityForName("IPodSongs", inManagedObjectContext: context) as! NSManagedObject
newSong.setValue("\(song.valueForProperty(MPMediaItemPropertyTitle))", forKey: "title")
println(newSong)
context.save(nil)
}
}
好的,您创建一个对象var newSong=..
,然后在每个循环迭代中覆盖它的属性。解决方案不是很明显吗?请不要在save中传递nil
。传递一个错误指针并检查错误。始终!当然您的意思是放置if context.save(&errorPointer==false{printlin>)(“收到错误)}
结尾正确吗?:)是的,但询问者似乎并不关心保存上下文时是否有错误……或者是否未受过教育。这是教育他们错误至关重要的绝佳机会。传递nil
的示例会导致他们忽视其价值。谢谢各位。我是Swift的新手(一般编程才一个月)所以我感谢你的帮助。上一篇if context.save{}
方法。如果我把它放在false
后面,我会得到一个未解析的标识符errorPointer
@MarcusS.Zarra我能得到这个errorPointer
的确认吗?我仍然会得到一个缺少括号的错误。