2014-11-23 95 views
0

我計劃在我的項目中使用幾個大型數組,並將數組存儲在單獨的文件中。這可能嗎? 這裏是與陣列「GRUPPE」我想有一個單獨的文件我的主視圖控制器例如:如何將數組存儲在單獨的文件中?

import UIKit 
class ViewController: UIViewController { 
var gruppe = ["new york", "sydney", "paris"] 
@IBAction func knopf(sender: UIButton) { 
println(gruppe[0]) 
} 
} 

回答

0

You can bridge a Swift array to a NSArray

做這樣的事情:

let cocoaArray : NSArray = gruppe 
cocoaArray.writeToFile("pathToYourFile", atomically:true) 
+0

對不起,不是外部文件,只在第二SWIFT文件。 – Yvana 2014-11-23 12:40:59

+0

你的問題很混亂。你想在Swift中創建一個* duplicate *數組嗎? – 2014-11-23 12:45:14

+0

我假設他只是想從邏輯中解耦數據。完全合情合理。 – Grimxn 2014-11-23 12:49:14

1

你可以嘗試這樣的:

// File 1 
import UIKit 
class ViewController: UIViewController { 

    @IBAction func knopf(sender: UIButton) { 
     println(DataClass.gruppe()[0]) 
    } 
} 

// File 2 
class DataClass { 
    class func gruppe() -> [String] { 
     return ["new york", "sydney", "paris"] 
    } 
} 
相關問題