'Looping through results with "for in" loop to build separate array
SWIFT COREDATA I'm having trouble setting up a for - in loop to collect results from a fetch. No problem getting individual results by fetch results[index] but inserting for-in loop gives me an error (type mismatch) or (type does not contain member in Generator). I want to use the fetch results to populate a separate array.
Any help you can provide is greatly appreciated.
excerpt from code:
import Foundation
import UIKit
import CoreData
// globaals
var pickResult:String?
var pickMultiresult:[AnyObject]?
// add var
var fullnameMulti:[String]? // array of full names
var i:Int = 0
// end new var
class ClientPicker:UIViewController, UIPickerViewDelegate {
// connections
@IBOutlet weak var singleResult: UITextField!
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
@IBOutlet weak var firstname: UITextField!
@IBOutlet weak var status: UILabel!
var lastname:String = ""
var phone:String = ""
var email:String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
// MARK: ********** block 2 --- findClient ****
@IBAction func findClient(sender: AnyObject) {
let entityDescription = NSEntityDescription.entityForName("Contacts", inManagedObjectContext: managedObjectContext)
let request = NSFetchRequest()
request.entity = entityDescription
let pred = NSPredicate(format: "(firstname = %@)", firstname.text!)
request.predicate = pred
do {
var results = try managedObjectContext.executeFetchRequest(request)
multiresult = results
if results.count > 0 {
// let match = results[0] as! NSManagedObject NOTE: these return correct data for indices 0 and 3
// let match = results[3] as! NSManagedObject NOTE: these return correct data for indices 0 and 3
status.text = "There are \(results.count) people named \(firstname.text!)"
} else {
status.text = "No Match"
}
} catch let error as NSError {
status.text = error.localizedFailureReason
}
// for in loop to populate firstnameMulti array
i = 0
for i in results{
var p = i
let match = results[p] as! NSManagedObject
/* the following code returns correct fullname when used outside of for loop
but error when I use the for loop */
firstname.text = match.valueForKey("firstname") as? String
/***************** concatenate first and last names ********/
let first = match.valueForKey("firstname") as? String
let last = match.valueForKey("lastname") as? String
// concatenate
var fullname:String = ""
fullname = "\(first!) \(last!)"
singleResult.text = fullname
}
}
*/ error: value of type string has no member in Generator */
Solution 1:[1]
You are filling the text of a text field in the loop and overwriting it at every Iteration. That does not make any sense.
To generate an array of full names, fetch your objects and cast to the proper NSManagedObject subclass, such as Person so you get a [Person] array and then you can simply use map:
let nameList = results.map { "\($0.firstname) \($0.lastname)" }
Solution 2:[2]
Several issues here but I will focus on the ones that you asked about.
First, you have declared i as a Int and then you are trying to use it in the for in loop. That is going to fail.
Second, you should be handling your type casting as part of the fetch:
var results = try managedObjectContext.executeFetchRequest(request) as! [NSManagedObject]
Now you have a collection that is typed as NSManagedObject and your for in becomes a lot cleaner:
for match in results {
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mundi |
| Solution 2 | Marcus S. Zarra |
