do {
let results = try context.executeFetchRequest(request)
for result in results as! [NSManagedObject] {
var savepassword = result.valueForKey("savePassword")!
}
} catch {
}
// what I want is to be able to access the variable out here
// savepasword
2 Answers
You need to declare savePassword outside the loop. You should note that if there are multiple objects returned from your query then you will end up with the value from the last object in the array. This may or may not be what you want
var savepassword : String?
do {
let results = try context.executeFetchRequest(request)
for result in results as! [NSManagedObject] {
savepassword = result.valueForKey("savePassword") as? String
}
} catch {
}
if let savepassword = savepassword {
// Do something with savepassword
}
1 Comment
Eendje
Shouldn't
result.valueForKey("savePassword") be casted to String?