ios - 在元组中追加值

标签 ios swift tuples

任何人都可以告诉我如何从我的 favBt 函数在 tuple 中附加值。 我尝试了两种不同的方法但出现同样的错误?

fatal error: unexpectedly found nil while unwrapping an Optional value

1. appendData.tupple.append(values)
2. appendData.tupple.append(url,image)

enter image description here

var objects : [(String, String)] = [
                ("https://www.youtube.com/watch?v=9h30Bx4Klxg","1.jpg"),
                ("https://www.youtube.com/watch?v=ij_0p_6qTss","2.jpg"),
                ("https://www.youtube.com/watch?v=AJtDXIazrMo","3.jpg"),
                ("https://www.youtube.com/watch?v=H202k7KfZL0","4.jpg"),
                ("https://www.youtube.com/watch?v=CGyEd0aKWZE","5.jpg"),
                ("https://www.youtube.com/watch?v=AtKZKl7Bgu0","6.jpg"),
                ("https://www.youtube.com/watch?v=4SYlLi5djz0","7.jpg")]

    func favBt(sender: UIButton){

            var btn : NSInteger
            btn = sender.tag as NSInteger

            let (url,image) = objects[btn]

            let values = (url, image)
            print(values)

            let appendData = ViewController()

            appendData.tupple.append(values)

            print(appendData.tupple[btn])

          }

    In my ViewController Class

    class ViewController: UIViewController{
       var tuple : [(String, String)]?
    }

最佳答案

letappendData = ViewController() 我从函数外部创建它。

替换它var tuple : [(String, String)]?

var 元组:[(String, String)] = []

关于ios - 在元组中追加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37134396/

相关文章:

python - 访问 python 元组中的特定条目

ios - 如何解决xcode中的iPhone应用程序内存泄漏(仪器)

iOS 应用程序在 Testflight 中显示为灰色图标

ios - Collection View 单元格边框颜色在 Swift 3 中取消选择

ios - deleteCharactersInRange:范围或索引超出范围

ios - 类型 'Int' 没有下标成员

python - NumPy - 从元组到数组的高效转换?

ios - 隐藏 QuickType 预测建议栏时,UITextView 失去自动更正功能

swift - 如何在 UIView 中居中放置多个元素

c++ - 查看 std::vector<std::tuple<Ts...>> 中的所有 T