4

I'm trying to put the results of a fetch request into an array. My code:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext
    let fetchRequest = NSFetchRequest(entityName: "CLIENTS")
    var mobClients = [NSManagedObject]()
    var arrayAllPhoneNumbers = [String]()

    do {
        let results = try managedContext.executeFetchRequest(fetchRequest)
        mobClients = results as! [NSManagedObject]

        for clientPhoneNumber in mobClients {

            let myClientPhoneNumber = clientPhoneNumber.valueForKey("clientsMobilePhoneNumber") as! String
            print(myClientPhoneNumber)
            //The numbers print out just fine, one below the other
            //
            //Now the results need to go into the array I've declared above ---> arrayAllPhoneNumbers

            messageVC.recipients = arrayAllPhoneNumbers // Optionally add some tel numbers

        }

    } catch
        let error as NSError {
            print("Could not fetch \(error), \(error.userInfo)")
    }

As illustrated, all the phone numbers needs to be captured in an array. How do I accomplish that?

2
  • You would be better served creating NSManagedObject sub-classes that represent your core data entities, Additionally, if you only want the phone numbers you should use a predicate in your initial fetch and then you can assign the resulting array of phone numbers directly. Commented Feb 28, 2016 at 18:20
  • That's a lot of (overwhelming) info for a n00b like myself! Give me a day or two to google all that, and if you don't mind, I would like to come back to you on this. Thanks for the response in the mean time! Commented Feb 28, 2016 at 18:41

3 Answers 3

4

Instead of your for-loop and the code inside it, use this:

arrayAllPhoneNumbers = mobClients.map({ clientPhoneNumber in
    clientPhoneNumber.valueForKey("clientsMobilePhoneNumber") as! String
})
messageVC.recipients = arrayAllPhoneNumbers
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm! Thank you very much!
1
let request = NSFetchRequest(entityName: "CLIENTS")
let results = (try? managedContext.executeFetchRequest(request)) as? [NSManagedObject] ?? []

let numbers = results.flatMap { $0.valueForKey("clientsMobilePhoneNumber" as? String }

numbers is now an array of your phone numbers.

But like thefredelement said, it's better to subclass it so you can just cast it to that subclass and access the phone numbers directly.

Comments

0

Swift 5 : flatMap is deprecated, use compactMap

let request = NSFetchRequest(entityName: "CLIENTS")
let results = (try? managedContext.executeFetchRequest(request)) as? [NSManagedObject] ?? []

let numbers = compactMap { $0.valueForKey("clientsMobilePhoneNumber" as? String }

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.