为了自学一些,我正在学习一些旧的教程。现在我在Objective C教程中找到了以下代码行:
Recipe *recipe = [recipes objectAtIndex:indexPath.row];
我正在尝试将其重写一段时间,但似乎无法使其正常工作。我尝试了以下方法:
var recipe: Recipe = recipes[indexPath.row] as Recipe
我的viewController.swift的完整来源:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var recipes: NSArray = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let recipe1: [Recipe] = [Recipe(
name: "Egg Benedict",
preptime: "30 min",
imageFile: "egg_benedict.jpg",
ingredients: ["2 fresh English muffins", "4 eggs", "4 rashers of back bacon", "2 egg yolks", "1 tbsp of lemon juice", "125 g of butter", "salt and pepper"])]
let recipe2: [Recipe] = [Recipe(
name: "Mushroom Risotto",
preptime: "25 min",
imageFile: "mushroom_risotto.jpg",
ingredients: ["1 tbsp dried porcini mushrooms", "2 tbsp olive oil", "1 onion, chopped", "2 garlic cloves", "350g/12oz arborio rice", "1.2 litres/2 pints hot vegetable stock", "salt and pepper", "25g/1oz butter"])]
recipes = [recipe1, recipe2]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return recipes.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellID = "RecipeCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellID)
cell!.accessoryType = .DisclosureIndicator
}
let recipe = recipes[indexPath.row] as Recipe
cell?.textLabel?.text = recipe.name
cell?.detailTextLabel?.text = recipe.preptime
return cell!
}
}
我的食谱课程:
import Foundation
class Recipe {
var name: String
var preptime: String
var imageFile: String
var ingredients: NSArray
init (name: String, preptime: String, imageFile: String = "", ingredients: NSArray = []) {
self.name = name
self.preptime = preptime
self.imageFile = imageFile
self.ingredients = ingredients
}
}
这至少是我的想法。创建一个属于我的Recipe类的变量。然后我将配方数组中的每个配方分配给tableView的行。
到目前为止,我的文件中出现零错误,我可以继续使用以下几行:
cell?.textLabel?.text = recipe.name
cell?.detailTextLabel?.text = recipe.preptime
它还可以在数组中找到名称和预处理时间,以便一切正常。仍为零错误。然后,当我启动我的应用程序时,会出现以下错误:
0x108ba88ad: movq %rdi, %rax
好吧,因为我不是调试专家,我无法修复它。我知道这就是导致我错误的那条线,因为如果我禁用它,我的应用程序就会运行得很好。
那么我做错了什么?
答案 0 :(得分:2)
当您考虑创建recipe1和recipe2时,实际上您创建的数组包含您的第一个配方和另一个包含第二个配方的数组。然后,将这两个阵列添加到另一个阵列。
如果您在[
和]
中放置了某些内容,则会将其转换为包含括号之间的内容的数组。
let recipe1: [Recipe] = [Recipe()] // an array with a Recipe
^ ^ ^ ^
let recipe2: [Recipe] = [Recipe()] // another array with a Recipe
^ ^ ^ ^
recipes = [recipe1, recipe2] // an array with two arrays that each contain a Recipe
删除阵列括号,你很好:
let recipe1 = Recipe(
name: "Egg Benedict",
preptime: "30 min",
imageFile: "egg_benedict.jpg",
ingredients: ["2 fresh English muffins", "4 eggs", "4 rashers of back bacon", "2 egg yolks", "1 tbsp of lemon juice", "125 g of butter", "salt and pepper"])
答案 1 :(得分:1)
有两种解决方案:
1)在let recipe = recipes[indexPath.row][0] as Recipe
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
2)在您的viewDidLoad
函数中将您的食谱声明更改为
let recipe1 = Recipe(
name: "Egg Benedict",
preptime: "30 min",
imageFile: "egg_benedict.jpg",
ingredients: ["2 fresh English muffins", "4 eggs", "4 rashers of back bacon", "2 egg yolks", "1 tbsp of lemon juice", "125 g of butter", "salt and pepper"])
let recipe2 = Recipe(
name: "Mushroom Risotto",
preptime: "25 min",
imageFile: "mushroom_risotto.jpg",
ingredients: ["1 tbsp dried porcini mushrooms", "2 tbsp olive oil", "1 onion, chopped", "2 garlic cloves", "350g/12oz arborio rice", "1.2 litres/2 pints hot vegetable stock", "salt and pepper", "25g/1oz butter"])