我目前有一个包含27个成就数据的plist文件,我正在尝试将其输入到核心数据实体中,如下所示:
func createAchievements() {
let A:Achievement = NSEntityDescription.insertNewObject(forEntityName: "Achievement", into: CoreDatabaseContoller.getContext()) as! Achievement
let filePath = Bundle.main.path(forResource: "Achievement", ofType: "plist")
let Items = NSArray(contentsOfFile: filePath!) as! [[String:AnyObject]]
for item in Items {
A.theName = item["Name"] as! String
A.theDescription = item["Description"] as! String
A.theImage = item["Image"] as! String
A.aAmount = item["Amount"] as! NSNumber
A.aNeededAmount = item["NeededAmount"] as! NSNumber //DOESN'T WORK IT ONLY SHOWS OR PUTS IN THE LAST ONE.
A.aStage = item["Stage"] as! NSNumber
A.aReward = item["Reward"] as! NSNumber
A.aHasBeenAchieved = item["HasBeenAchieved"] as! NSNumber
A.theSearchName = item["SearchName"] as! String
CoreDatabaseContoller.saveContext()
}
}
但是当我加载游戏和显示成就的游戏的成就菜单部分时,所有应该由核心数据填充的图像和文本都不存在,并且图像是红色的X,除了最后的成就如下图所示,成就菜单似乎正常工作
我最初通过一个在我的代码中看起来很糟糕的交换机系统从成就中输入数据,如下所示,它没有问题。但是现在由于某种原因,当我尝试从plist到核心数据输入它时,似乎无法正常工作
func giveDataForAchievements(){
if achievementsGenerated == false {
for count in 1...24 {
var aName = "None"
var aDesc = "None"
var aImage = "locked"
var aAmount:NSNumber = 0
var aNeededAmount:NSNumber = 1
var aStage:NSNumber = 0
var aReward:NSNumber = 0
var aHasbeenAchieved:NSNumber = 0
var aSearchName = ""
switch count {
case 1:
aName = "DAILY"
aDesc = "Daily Achievement"
aImage = "locked"
aAmount = 0
aNeededAmount = 1
aStage = 0
aReward = 350
aHasbeenAchieved = 0
aSearchName = "Ach0"
case 2:
aName = "DAILY"
aDesc = "Daily Achievement"
aImage = "locked"
aAmount = 0
aNeededAmount = 1
aStage = 0
aReward = 350
aHasbeenAchieved = 0
aSearchName = "Ach1"
case 3:
aName = "DAILY"
aDesc = "Daily Achievement"
aImage = "locked"
aAmount = 0
aNeededAmount = 1
aStage = 0
aReward = 350
aHasbeenAchieved = 0
aSearchName = "Ach2"
case 4:
aName = "ROOKIE"
aDesc = "Finish the tutorial level"
aImage = "locked"
aAmount = 0
aNeededAmount = 1
aStage = 2
aReward = 350
aHasbeenAchieved = 0
aSearchName = "Ach3"
createAchievements(aName: aName, Des: aDesc, Image: aImage, Amount: aAmount, Needed: aNeededAmount, Stage: aStage, Reward: aReward, BeenAchieved: aHasbeenAchieved, SearchName: aSearchName)
// all the way to 27
我不知道如何解决这个问题。谁能告诉我我做错了什么?
答案 0 :(得分:1)
您要多次更新同一个托管对象。移动线:
let A:Achievement = NSEntityDescription.insertNewObject(forEntityName: "Achievement", into: CoreDatabaseContoller.getContext()) as! Achievement
进入for循环。然后它将为每个项目创建一个新对象。