正在初始化密钥的值-Swift



这里我试图在表视图中进行搜索。这把我难住了。我似乎不知道如何初始化"name"的值,以便搜索它。https://github.com/Brandon-316/WOD-Guide

图书馆样本代码:

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-upsn30 Push-upsn40 Sit-ups50 SquatsnnRest precisely three minutes between each round.",
            "description": "5 rounds, time each round"
        ],
        [
            "name": "Chelsea",
            "exercises": "5 Pull-upsn10 Push-upsn15 Squats",
            "description": "Each min on the min for 30 min"
        ],
        [
            "name": "Cindy",
            "exercises": "5 Pull-upsn10 Push-upsn15 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
    }
}

它在哪行崩溃?看起来你确实有潜在的崩溃:

    let result = results.name
    filteredWODS.append(result!)

其中results最初定义时没有有效密钥:

    var results = WOD(dictionary:["": ""])

我还是不明白你的目标。我建议将其提炼成一个简单的游乐场,在没有视图控制器的情况下演示您的问题。

如果我获取此代码并运行它,它会在上面提到的行中崩溃。

为了使其不崩溃,您必须删除result!上的强制展开,并使用可选绑定(if let...)。

最新更新