1
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 2

3

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Shouldn't result.valueForKey("savePassword") be casted to String?
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")!
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.