I'm pretty new to Swift, and I've managed to get pretty stuck.
I'm trying to retrieve data from NSUserDefaults
and store it in an array (tasks
):
@lazy var tasks: NSArray = {
let def = NSUserDefaults.standardUserDefaults()
let obj: AnyObject? = def.objectForKey("tasks")
return obj as NSArray
}()
All I'm getting is a warning: EXE_BAD_INSTRUCTION
on line 3.
Also to note that I haven't actually set any data yet, but what I'm aiming for is that if there is no data, I want the array to be empty. I'll be using the data to populate a table view.
Now using a var instead of a constant:
@lazy var tasks: NSArray = {
let def = NSUserDefaults.standardUserDefaults()
var obj: AnyObject? = {
return def.objectForKey("tasks")
}()
return obj as NSArray
}()
The error has now moved to the return line.