Я пытаюсь подключить данные, выводимые фильтрами Realm.Я не могу и получаю сообщение об ошибке при попытке.
Я попытался определить вывод «nutrition» как строку, int и float.
Вот мой код:
import UIKit
import RealmSwift
class SecondViewController: UIViewController, UITableViewDataSource {
var localNumNutrition: Int = 0
var localNumFoodType: Int = 0
var localNutrition: String = ""
var localFoodType: String = ""
var nutritionRealmData = ["Energ_Kcal", "Protein_g", "Fiber_TD_g", "Sugar_Tot_g", "Calcium_mg", "Iron_mg", "Sodium_mg", "Vit_D_IU", "Cholestrl_mg"]
func localizeInput() {
localNumNutrition = numNutrition
localNumFoodType = numFoodtype
localNutrition = nutrition
localFoodType = foodtype
}
let testItems = ["Item A: protein", "Item B", "Item C"]
//var displayItems: [String] = ["No Values: 00.00"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return testItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = testItems[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
localizeInput()
// Do any additional setup after loading the view, typically from a nib.
/**Realm
let realm = try! Realm()
print(Realm.Configuration.defaultConfiguration.fileURL)
Bundle.main.path(forResource: _, ofType:)
*/
func setDefaultRealmForUser(username: String) {
var config = Realm.Configuration()
// Use the default directory, but replace the filename with the username
config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("\(username).realm")
// Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = config
}
let config = Realm.Configuration(
// Get the URL to the bundled file
fileURL: Bundle.main.url(forResource: "NutritionData", withExtension: "realm"),
// Open the file in read-only mode as application bundles are not writeable
readOnly: true)
//print(config)
//print(Realm.Configuration.defaultConfiguration.fileURL)
// Open the Realm with the configuration
let realm = try! Realm(configuration: config)
localNutrition = nutritionRealmData[localNumNutrition]
// Read some data from the bundled Realm
let results = realm.objects(NutritionData.self).filter("App_Food_Pyramid_Category =" + String(localNumFoodType)).sorted(byKeyPath: localNutrition, ascending: false)
let nutritionValuesResults = results.value(forKeyPath: localNutrition)
let shortDesc = results.value(forKeyPath: String("Shrt_Desc"))
//print(results)
// print(shortDesc)
// print(nutritionValuesResults)
/*
for (let i = 0; results.length - 1; i++) {
let item = results[i];
}
*/
var itemCount: Int = 0
for item in results {
itemCount = itemCount + 1
if itemCount < 10 {
let desc = String(item.Shrt_Desc!)
let nutritionValue = item.value(forKeyPath: localNutrition) **These are the 2 outputs**
//let stringNutritionValue = NSNumber(nutritionValue)
let displayItems = [(String(desc) + String(nutritionValue)] **Trying to join them to get a single output.**
//print(displayItems)
print(desc)
print(nutritionValue)
}
}
//print(item)
/*
var iterator = results.makeIterator()
let NutritionDataNext = iterator.next()
print(next)
*/
navigationController?.navigationBar.prefersLargeTitles = true
}
}
Я пытаюсь объединить desc (description) и nutritionValue (decimal) в один массив для использования в качестве данных для табличного представления.Я пытаюсь заставить данные отображаться как выходные данные desc: nutritionValue.