如何将结构体的实例命名为变量的内容 - Swift [英] How to name an instance of a struct the contents of a variable - Swift

查看:31
本文介绍了如何将结构体的实例命名为变量的内容 - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎可以肯定有更好的方法来做到这一点,我很想知道,但我无法在问题中表达出来,所以基本上这是我的问题:

There is almost certainly a better way of doing this and I'd love to know but I can't phrase it in a question so essentially here is my problem:

我正在创建一个显示项目列表(在表格视图中)的应用程序,其中包含与项目(String Int Date 等)一起出现的各种数据位.我决定将这些数据存储在结构中的最佳方式,因为它允许我存储丢失的不同类型的数据以及在其上运行进程.

I am creating an app that presents a list of items(In a table view) which have various bits of data that come along with the item(String Int Date ect). I've decided that the best way to store this data is in a struct because it allows me to store lost of different types of data as well as run processes on it.

问题是理论上我希望列表中有无限数量的项目,因此我需要在不预先确定每个实例的名称的情况下丢失 Item 结构的实例.

The problem is that I want to have theoretically an infinite number of items in the list and so I need to make lost of instances of the Item struct without predetermining the names of each instance.

然后我会将这些实例名称存储在一个数组中,以便它们可以在表视图中列出.

I would then store these instance names in an array so that they can be listed in the table view.

此时我完全被困住了,我花了几个小时寻找,但我无法理解它我确信它非常简单,因为必须有数百个应用程序需要这样做.我愿意接受任何事情,谢谢.

I'm completely stuck at this point I've spent hours looking and I just can't make sense of it I'm sure its stupidly easy because hundreds of apps must need to do this. I'm Open to anything thanks.

目前,我有一个结构:

struct Item() {
    var data1: String
    var data2: String // (But Should be Int)
    var data3: String

    func setDate() {
        // code
    }

    func returnDate() {
        // code
    }
}

然后在视图控制器中我有:

and then in the view controller I have:

@IBAction func SubmitButton(_ sender: UIButton) {
    var textField1 = Item(data1: textField1.text!, data2: textFeild2.text!, data3: "Units")
    print(textField1.data1)
}

推荐答案

我不完全确定您的目标是什么,但我想最终目标是拥有一个您的项目对象数组,其中然后可以用来填充 UITableView???没有为此实例设置名称?

I am not completely sure what your goal is but I guess the final goal is to have an array of your Item Objects, which then can be used to populate an UITableView??? Without setting up a name for this Instances?

最简单的解决方案是创建一个数组作为类属性来存储所有空的项目,如下所示:

The most easiest solution would be to create a array as a Class Property for storing all the Items which is empty like this:

var items : [Item] = []

然后在 viewDidLoad() 中,您可以调用一个函数,用所有可用的项目填充项目数组.然后可以使用此 Item Array 填充 UITableView.

And then in viewDidLoad(), you can call a function which populates the item array with all the available Items. This Item Array can then be used to populate the UITableView.

第二部分:你得到了这个 IBAction,它创建了一个新项目.我想这应该是在现有项目中添加一个新项目.

Second Part: You got this IBAction which creates a new Item. I guess this is supposed to add a new Item to the existing ones.

您可以使用

items.append(Item(<parameters>))

并且不要忘记重新加载 UITableView 以包含新数据.

And don't forget to reload the UITableView to include the new data.

tableView.reloadData()

这篇关于如何将结构体的实例命名为变量的内容 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆