Здесь я пытаюсь выполнить поиск в виде таблицы. Это меня насторожило. Я просто не могу понять, как инициализировать значение «имя», чтобы я мог его искать. https://github.com/Brandon-316/WOD-GuideИнициализация значения ключа - Swift
библиотека Пример кода:
import Foundation
struct BenchmarkWODs {
static let library = [
[
"name": "Angie",
"exercises": "100 Pull-ups/n100 Push-ups/n100 Sit-ups/n100 Squats",
"description": "For Time Complete all reps of each exercise before moving to the next."
],
[
"name": "Barbara",
"exercises": "20 Pull-ups\n30 Push-ups\n40 Sit-ups50 Squats\n\nRest precisely three minutes between each round.",
"description": "5 rounds, time each round"
],
[
"name": "Chelsea",
"exercises": "5 Pull-ups\n10 Push-ups\n15 Squats",
"description": "Each min on the min for 30 min"
],
[
"name": "Cindy",
"exercises": "5 Pull-ups\n10 Push-ups\n15 Squats",
"description": "As many rounds as possible in 20 min"
]
]
}
Подробный код файла:
import Foundation
import UIKit
struct WOD {
var name: String?
var exercise: String?
var description: String?
init(dictionary: [String: String]) {
name = dictionary["name"]
exercise = dictionary["exercises"]
description = dictionary["description"]
}
}
ViewController код:
import UIKit
class BenchmarkWODViewController: UITableViewController,
UISearchResultsUpdating {
var wodList = [WOD]()
var searchController : UISearchController?
var resultsController = UITableViewController()
var filteredWODS = [String]()
var results = WOD(dictionary:["name": ""])
override func viewDidLoad() {
super.viewDidLoad()
for wodData in BenchmarkWODs.library {
let wod = WOD(dictionary: wodData)
wodList.append(wod)
}
// Search Bar
self.searchController = UISearchController(searchResultsController: self.resultsController)
self.tableView.tableHeaderView = self.searchController?.searchBar
self.searchController?.searchResultsUpdater = self
}
// Search Bar Function
func updateSearchResultsForSearchController(searchController: UISearchController) {
let result = results.name
filteredWODS.append(result!)
filteredWODS = self.filteredWODS.filter { (result:String) -> Bool in
return true
}
self.resultsController.tableView.reloadData()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return wodList.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("BenchmarkCell", forIndexPath: indexPath) as UITableViewCell
let wod = wodList[indexPath.row]
if let wodName = wod.name {
cell.textLabel?.text = wodName
}
return cell
}
}
Почему вы ненавидите отступы? – Alexander
Я смотрю на функцию 'updateSearchResultsForSearchController', и в ней есть строка' let result = results.name'. Тем не менее, 'results' свободен в функции и ссылается на свойство класса' var results = WOD (dictionary: ["": ""]) '. Скомпилирует ли этот код? – Feldur
Да, он компилируется. Однако он падает при поиске, если я не использую имя в результатах. var results = WOD (словарь: ["name": ""]) –