简体   繁体   English

如何在核心数据中存储数组(Swift)

[英]How do you store an array in core data (Swift)

My code reads a text file and stores the contents of the file in an array. 我的代码读取一个文本文件,并将文件的内容存储在一个数组中。 I'm having trouble with the next step; 我在下一步遇到麻烦; transferring the contents of the array into Core Data. 将数组的内容传输到Core Data中。 The .txt file is just a short list of fruits. .txt文件只是水果的简短列表。 The entity is "Fruit" and the attribute is "fruitname". 实体为“水果”,属性为“水果名称”。

Only the last array element is showing up when I print. 打印时仅显示最后一个数组元素。 Here's my code:- 这是我的代码:-

    import UIKit
    import CoreData

    class ViewController: UIViewController {

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    // STORE .TXT FILE IN ARRAY.....
    let bundle = NSBundle.mainBundle()
    let fruitList = bundle.pathForResource("List of Fruits", ofType: "txt")

    let fruitArray = String(contentsOfFile: fruitList!, encoding: NSUTF8StringEncoding, error: nil)!.componentsSeparatedByString("\r")


    for x in fruitArray {
        println(x)

    }

    // STORE FRUIT-ARRAY IN CORE DATA......
    var appDel = UIApplication.sharedApplication().delegate as AppDelegate

    var context : NSManagedObjectContext! = appDel.managedObjectContext!

    var newFruit = NSEntityDescription.insertNewObjectForEntityForName("Fruit", inManagedObjectContext: context) as NSManagedObject

    for fruit in fruitArray {

        newFruit.setValue(fruit, forKey: "fruitname")
    }

    context.save(nil)


    // RETRIEVE AND PRINT STORED VALUES....
    var request = NSFetchRequest(entityName: "Fruit")
    request.returnsObjectsAsFaults = false

    var results = context.executeFetchRequest(request, error: nil)

    println(results)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

This is the output from both println statements.... 这是两个println语句的输出。

// Println of fruitArray Apple Apricot Banana Bilberry Blackberry Blueberry Coconut Cranberry Date Dragonfruit Fig Grape Guava Honeydew Kiwi Lemon Lime Lychee Mango Melon Orange Papaya Pineapple Raspberry Star fruit Strawberry Watermelon //水果的Println苹果杏香蕉越桔黑莓蓝莓椰子蔓越莓日期龙果无花果葡萄番石榴甘露猕猴桃柠檬石灰荔枝芒果瓜橙色番木瓜菠萝覆盆子杨桃草莓西瓜

//Println of core data //打印核心数据

Optional(........ fruitname = Watermelon; })] 可选(........ fruitname =西瓜;})]

Can someone please help make sure that everything in fruitArray gets saved in core data? 有人可以帮忙确保FruitArray中的所有内容都保存在核心数据中吗? Thanks in advance. 提前致谢。

You have only created one newFruit . 您仅创建了一个newFruit Thus, your for fruit in fruitArray loop is just repeatedly reassigning the fruitname property. 因此,您for fruit in fruitArray循环中的for fruit in fruitArray只是重复地重新分配了fruitname属性。

Change your code to: 将您的代码更改为:

for fruit in fruitArray {
  var newFruit = NSEntityDescription.insertNewObjectForEntityForName ("Fruit", 
     inManagedObjectContext: context) as NSManagedObject

  newFruit.setValue(fruit, forKey: "fruitname")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM