-1

I'm reasonably new to swift, im making a simple class to read and write data from a .plist. I cannot understand why I am getting this compiler error when declaring these constants.

class Data {

let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentDirectory = paths[0] as! String 
}

error:

'Data.type' does not have a member named 'paths'
Blake Lockley
  • 2,931
  • 1
  • 17
  • 30

1 Answers1

1

You cannot set the value of a property based on another property. There's no guarantee that they will be initialized in any order. The only guarantee is that class properties are initialized before instance properties, hence the Data.type in the error message. It refers to the collection of all class properties.

There are many ways to resolve this. Here's mine:

class Data {

var documentDirectory : String!

init () {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
    documentDirectory = paths[0] as! String 
}

}
Code Different
  • 90,614
  • 16
  • 144
  • 163