1

I have a class with an array which values comes from a text file. I would like to read this values once and store them in a shared variable, making possible other classes access that values.

How can I do that in Swift?

UPDATE:

Suppose I have three classes of animals and which of them can be found in a set of places which is load from differents tables (each animal have yours and the structure is different for each one). I would like to be able to use them linking to specific class:

  • clBirds.Places
  • clDogs.Places
  • clCats.Places

Note that I need to load data once. If I dont´t have a shared variable and need to put it outside the class, I need to have different names to the methods, just like:

  • BirdsPlaces
  • DogsPlaces
  • CatsPlaces

And we don´t have heritage in this case

Nizam
  • 4,569
  • 3
  • 43
  • 60
  • 1
    Same answer, but now you should be using an enum (or a struct) at the top level of a file. All other files (classes) can see it, and presto, you've got constants. I've been doing this, for example, with all my NSUserDefaults keys. In other words, now that you are using Swift, try to think in Swift! Use Swift features more. – matt Jul 02 '14 at 14:38
  • Your example is awesome!!! You are right, I need to change my paradigm to think in Swift. I have the same problem with English - I read,write and speak English thinking in Portuguese. ;-) – Nizam Jul 02 '14 at 15:08

1 Answers1

3

Declare the variable at the top level of a file (outside any classes).

NOTE: variables at the top level of a file are initialized lazily! So you can set the default value for your variable to be the result of reading the file, and the file won't actually be read until your code first asks for the variable's value. Cool!

Similarly, you can declare an enum or struct at the top level of a file (outside any classes), and now it is global. Here's an example from my own code:

struct Sizes {
    static let Easy = "Easy"
    static let Normal = "Normal"
    static let Hard = "Hard"
    static func sizes () -> String[] {
        return [Easy, Normal, Hard]
    }
    static func boardSize (s:String) -> (Int,Int) {
        let d = [
            Easy:(12,7),
            Normal:(14,8),
            Hard:(16,9)
        ]
        return d[s]!
    }
}

Now any class can refer to Sizes.Easy or Sizes.boardSize(Sizes.Easy), and so on. Using this technique, I've removed all the "magic numbers" and "magic strings" (such as NSUserDefault keys) from my code. This is much easier than it was in Objective-C, and it is much cleaner because (as the example shows) the struct (or enum) gives you a kind of miniature namespace.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • It is different. It is the same approach with static methods? Do I need to declare the method outside the class? Is it a good practice? – Nizam Jul 02 '14 at 05:28
  • I don't know what you mean by static. But it is very good practice for global utility functions. – matt Jul 02 '14 at 12:04
  • Why is 'sizes' a func instead of a a simple 'let'? (static let sizes = [ Easy, Normal, Hard]) – hnh Jul 03 '14 at 21:13
  • Also, board size should probably just use a switch instead of creating a dictionary on every invocation. – hnh Jul 03 '14 at 21:15