简体   繁体   English

将数组声明为类的属性,并在Swift中使用它

[英]Declare array as property of class and use it in Swift

I have a cooking app, where I have meal with name, and cooking steps, which are presented as an array of strings: 我有一个烹饪应用程序,其中有我的名字的饭菜和烹饪步骤,以字符串的形式显示:

class Meal {    
    var name: String
    var steps: Array<String>?
}

And then I try to pass the data to view: 然后,我尝试传递数据进行查看:

override func viewDidLoad() { 
    super.viewDidLoad()

    // Handle the text field’s user input through delegate callbacks.
    nameTextField.delegate = self

    // Set up views if editing an existing Meal.
    if let meal = meal {
        nameTextField.text = meal.name
    }
    createLabelAndText()
}

The createLabelAndText method should create for each element of an array label - something like Step 1, Step 2 and textView createLabelAndText方法应为数组label每个元素创建-类似于步骤1,步骤2和textView

  func createLabel() {

        for (index, element) in (meal?.steps){
            let myLabel = UILabel()

            //Assigning value or text to label
            myLabel.text = "Step \(index)"

            //Assigning frame to the label
            myLabel.frame = CGRect(x: 10, y: index*100, width: 200, height: 30)

            let textView = UITextView()
            textView.text = element
            textView.frame = CGRect(x: 10, y: index*100, width: 200, height: 30)

            //Finally we need to add label to view to display it on screen.
            self.view.addSubview(myLabel)
            self.view.addSubview(textView)
        }
    }

And what I want to get is something like this: [ http://a1.mzstatic.com/us/r30/Purple30/v4/24/3e/64/243e64b5-79cf-9a35-5386-d81ed250b926/screen696x696.jpeg][1] 我想得到的是这样的:[ http://a1.mzstatic.com/us/r30/Purple30/v4/24/3e/64/243e64b5-79cf-9a35-5386-d81ed250b926/screen696x696.jpeg] [1]

Right now I have a problem - I can't loop trough meal?.steps and get the error Type '[Any]?' does not conform to protocol 'Sequence' 现在,我遇到了一个问题-无法循环meal?.steps并得到错误Type '[Any]?' does not conform to protocol 'Sequence' Type '[Any]?' does not conform to protocol 'Sequence'

Also I try to configure a view controller before it's presented. 另外,我尝试在呈现视图控制器之前对其进行配置。 But it doesn't allow me to access and assign steps from class meal. 但这不允许我访问和分配课堂上的步骤。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    let name = nameTextField.text ?? ""
    let steps = meal?.steps
    // Set the meal to be passed to MealTableViewController after the unwind segue.
    meal = Meal(name: name, steps: steps )

}

How could I solve this problems or maybe there are some better approaches? 我该如何解决这个问题,或者有更好的方法?

You need to unwrap steps , because not only is meal an optional, but so is steps . 您需要解开steps ,因为不仅meal是可选的,而且steps Eg 例如

func createLabel() {
    guard let steps = meal?.steps else { return }

    for (index, element) in steps.enumerated() {
        let label = UILabel()

        //Assigning value or text to label
        label.text = "Step \(index)"

        //Assigning frame to the label
        label.frame = CGRect(x: 10, y: index*100, width: 200, height: 30)

        let textView = UITextView()
        textView.text = element
        textView.frame = CGRect(x: 10, y: index*100, width: 200, height: 30)

        //Finally we need to add label to view to display it on screen.
        view.addSubview(label)
        view.addSubview(textView)
    }
}

Obviously, if you want the index as you enumerate through steps , you need to use enumerated() , too. 显然,如果您希望在steps枚举时使用index ,则也需要使用enumerated()

As an aside, I'm not sure why you're setting your text view to have the same frame as the label, effectively putting it on top of the label, but that's beyond the scope of this question. 顺便说一句,我不确定为什么要将文本视图设置为与标签具有相同的frame ,从而将其有效地置于标签之上,但这超出了此问题的范围。 The main issue here is just to make sure you unwrap your steps optional before trying to use it. 此处的主要问题只是确保在尝试使用steps之前将其解包为可选steps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM