ios - Swift: Outputting a CoreData fetched Array of Dictionaries to a TableView -


So what I'm trying to achieve originally is one list (different) categories is obtained from Kordata a TableView categories is displayed, and fetchRequest.returnsDistinctResults = true alphabetical sorting variation through and filtered. Fetch is an array of dictionaries:

  ({class = bread;}, {class = cheese;}, {class = vegetables}})  
< P> I have some problems in outputting these range values ​​in my table view, as var distinctResults as zero as output func fetchMaterial () , resulting in: Fatal Error: When opening an optional value on this part in code unexpectedly found zero Expertise is:

  specific Wapasian! .count  

How can I be able to set the specific properties as my true value, such as func fetchProduct () Is set in? (Assuming this is the best way to solve this issue) Thousands thanks in advance! =)

This is my relevant code:

  import UIKit import CoreData class CategoryTableViewController: UITableViewController {lazy bridesmaid managedObjectContext: NSManagedObjectContext? = {. Let AppDelegate = UIApplication.sharedApplication () AppDelegate as representative if the managedObjectContext = appDelegate.managedObjectContext {return managedObjectContext} else {return null}} () var distinctResults: [string] Override function viewDidLoad () {super.viewDidLoad () fetchProduct ()} function fetchProduct () {go fetchRequest = NSFetchRequest (EntityName: "Product") // Create a type sorting the "Category" property descriptor object Core Data Object (: set "category", ascending true key) // list of descriptors such festive requests so it descriptors fetchRequest.sortDescriptors = [sortDescriptor] fetchRequest.propertiesToFetch = NSArray (including object "category" sortDescriptor = NSSortDescriptor go) fetchRequest .returnsDistinctResults = true fetchRequest.resultType = NSFetchRequestResultType.DictionaryResultType d IstinctResults: NSArray = managedObjectContext .executeFetchRequest (fetchRequest, error: zero)! } Override Funk Receive Memory Warning () {super.didReceiveMemoryWarning () // Any Resource Disposal which can be redone. } // Mark: - table view data source overcride funk numberoffification intbox view (Tableview: UITABLE View) - & gt; Int {Return 1} Override Funk TableView (TableView: UITableView, numberOfRowsInSection section: Int) - & gt; Int {returns different results !. Count} Override Function Table View (Table View: UITableView, cellForRowAtIndexPath Index Page: NSIndexPath) - & gt; UITableViewCell {the cell = tableView.dequeueReusableCellWithIdentifier ( "cell") as CategoryTableViewCell go distinctResult = distinctResults [indexPath.row] // cell configured cell.categoryLabel? .text = distinctResult returned cell}}   

You can specify your ivar specific results < / Code> are not set. In fetchProduct , you type

  var distinctResults: NSArray = ...  

keyword var You are declaring a new variable, whose limit will be limited to the fetchProduct method, after returning the method, the variable will be abandoned.

Instead, you should assign the result to your current variable

  self.distinctResults = ...  

I think That there may be other problems with your code, but this is the answer about your question's zero variable


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -