Yvana
Yvana

Reputation: 23

How can I store an array in a separate file?

I'am planing to use a couple of large arrays in my project and like to store the arrays in a separate file. is this possible? here is my main viewcontroller example with the array "gruppe" I like to have in a separate file:

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

Upvotes: 0

Views: 660

Answers (2)

Grimxn
Grimxn

Reputation: 22487

You could try something like:

// 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"]
    }
}

Upvotes: 1

Michael Dautermann
Michael Dautermann

Reputation: 89509

You can bridge a Swift array to a NSArray.

doing something like:

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

Upvotes: 0

Related Questions