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
答案 0 :(得分:2)
您需要在循环外声明savePassword
。您应该注意,如果从查询中返回了多个对象,那么您将得到数组中最后一个对象的值。这可能是你想要的也可能不是你想要的
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 :(得分:0)
var any: Any
do {
let results = try context.executeFetchRequest(request)
any = results
} catch {
}
for result in any as! [NSManagedObject] {
var savepassword = result.valueForKey("savePassword")!
}