1

I'm pretty new to Swift/Xcode and I'm faced with a problem of trying to place CoreData attribute values into an array.

The app is a simple shopping list with an entity with 5 attributes:

entity is "Item"

  1. image Binary data
  2. name String
  3. note String
  4. qty String
  5. status Boolean

The user creates a new shopping item which is displayed in a UITableVIewController (so far I'm fine)

I have to data loading into cells correctly. I also want to display the total quantity(entity "qty") of items in a label.

I can't seem to put each qty into an array so that I can add them together to display them. There seems to be plenty of resources for Objective C but not much out there for swift.

These are what I've been looking at:

Filling an array with core data attributes

CoreData to Array in Swift

Swift: Fetch CoreData as Array

coredata - fetch one attribute into an array

UITableViewController Code:

    let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

    var frc2 : NSFetchedResultsController = NSFetchedResultsController()
    func fetchRequest2() -> NSFetchRequest {

        let fetchRequest2 = NSFetchRequest(entityName: "Item")
        let sortDescriptor2 = NSSortDescriptor(key: "name",ascending:true)
        fetchRequest2.sortDescriptors = [sortDescriptor2]
        return fetchRequest2
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //set frc
        frc2 = getFRC()
        frc2.delegate = self

        do {
            try frc2.performFetch()
        } catch {
            print("failed to perform initial fetch request")
            return
        }
    self.tableView.reloadData()
    }

Any help would be greatly appreciated.

1
  • Any reason qty is a string? It would be straight forward to sum if it were a number. Commented Apr 12, 2016 at 20:23

0

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.