我的代码读取一个文本文件,并将文件的内容存储在一个数组中。我在下一步遇到麻烦;将数组的内容传输到Core Data中。.txt文件只是水果的简短列表。实体为“水果”,属性为“水果名称”。
打印时仅显示最后一个数组元素。这是我的代码:-
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! = …Run Code Online (Sandbox Code Playgroud)