[英]Objective-C: Randomizer objectAtIndex Not sorting through All Types
[英]Objective-C to swift objectAtIndex:
为了快速自学,我正在学习一些旧教程。 现在,我在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
好吧,因为我不是调试专家,所以无法修复它。 我知道那是导致我出错的那行,因为如果我禁用它,我的应用程序将运行正常。
那我在做什么错?
当您认为自己创建配方1和配方2时,实际上是创建一个包含第一个配方的数组和另一个包含第二个配方的数组。 然后,将这两个数组都添加到另一个数组中。
如果在[
和]
放东西,它将变成一个数组,在括号之间包含该东西。
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)更改代码行,以在func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let recipe = recipes[indexPath.row][0] as Recipe
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"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.