ios - 如何在结构数组中插入数字(Swift4)

标签 ios arrays swift struct insert

我的代码不工作。我要做的就是将数字插入到结构的内部数组中。现在这不起作用:

@IBAction func move(_ sender: Any) {
    bad.numbers.insert(0, at: 0)
}


struct bad {
    var numbers: [Int] = [1, 2, 3]
}

最佳答案

您需要将数字属性声明为静态的。顺便说一下,以大写字母开头命名结构是 Swift 的惯例:

struct Bad {
    static var numbers: [Int] = [1, 2, 3]
}

要在索引 0 处插入元素,您需要这样调用它:

Bad.numbers.insert(0, at: 0)

print(Bad.numbers)  // "[0, 1, 2, 3]\n"

关于ios - 如何在结构数组中插入数字(Swift4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46556159/

相关文章:

ios - "This app could not be installed at this time"CFBundleIdentifier 错误

iphone - dismissViewControllerAnimated 不适用于 iPhone 5

ios - 根据另一个数组的顺序对数组进行高效排序

java - 从文件读入数组

ios - 从 NSData 字节获取浮点值

ios - 使用 CoreData 不保存对象

当导航栏隐藏时,iOS 个人热点会破坏布局

objective-c - 从对象内的数组值创建数组

arrays - 使用 Intent(in) 参数初始化 Fortran 参数

ios - Firebase 查询两个值