1

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 */

2 Answers 2

2

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

Comments

1

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 {

3 Comments

Thanks I changed these 2 statements and got rid of extraneous variables but now have another error. Can you point me to an example code of building an array from a fetch?
The first line of code I showed in my answer gives you the array you need. What error are you getting? Giving vague comments has not value and does not make it possible for anyone to give you assistance.
If you are going to vote me down at least leave a comment why you think my answer is wrong.

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.