下标语法可将值附加到数组中 [英] Subscript Syntax to append values in array

查看:32
本文介绍了下标语法可将值附加到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在《 Swift编程语言》一书中,它明确指出:

In the Swift Programming Language book it explicitly states:

您不能使用下标语法在数组末尾添加新项

You can't use subscript syntax to append a new item to the end of an array

书中提供的示例还指出,即使替换值集的长度与要替换的范围的长度不同,也可以.例如:

The example provided in the book also states that even if the replacement set of values has a different length than the range you are replacing it is fine. So for example:

var shoppingList = ["Eggs", "Milk", "Chocolate"]
shoppingList[0...2] = ["Bananas", "Apples"]
// shoppingList = ["Bananas", "Apples"]

之所以有意义,是因为您要用Banana和Apple的值替换3个值.但是,如果我们改为这样做:

which makes sense because you are replacing 3 values with the values of Banana and Apple. However, if we did this instead:

var shoppingList = ["Eggs", "Milk", "Chocolate"]
shoppingList[0...2] = ["Bananas", "Apples", "Sausage", "Pear"]
// shoppingList = ["Bananas", "Apples", "Sausage", "Pear"]

我认为我们不允许使用下标语法在数组末尾添加新项,但这似乎是该语言中的漏洞.有人可以向我解释为什么会发生这种情况,并且/或者如果这是有效的行为还是错误?谢谢!

I thought we were not allowed to use subscript syntax to append a new item to the end of the array, but this seems like a loophole in the language. Can someone explain to me why this happens and/if this is a valid behaviour or is a bug? Thanks!

推荐答案

当书中说您不能使用下标语法将新项目附加到数组末尾"时,实际上是指" Array.subscript(index:Int)->的方式T 已实现,您不能使用它将新项追加到数组的末尾."

When the book says "you can’t use subscript syntax to append a new item to the end of the array", really what it means is, "the way Array.subscript (index: Int) -> T has been implemented, you can’t use it to append a new item to the end of the array".

下标只是一个语法性质的函数,它具有一些带有一些参量(在本例中为 index )并具有值(在在这种情况下为 T ).但是就像任何函数一样,它可以执行任何您想要的操作.它可以将值设置为 index ,或者如果以这种方式编写,则可以发射核导弹.

subscript is just a bit of syntactic sugar for a function with propertyish qualities that takes some arguments (in this case index) and either gets or sets a value (in this case T). But just like any function, what it does can be whatever you want. It could set the value at index, or it could fire the nuclear missiles if you wrote it that way.

实际上, Array subscript 定义了第二个重载,该重载在范围上运行,并且确实具有扩展功能.但是,这本书专门指的是只采用单个索引的更传统的索引.

As it happens, Array defines a second overload for subscript that operates on ranges, and that does have extending capabilities. But the book is referring specifically to the more conventional one that just takes a single index.

为了演示,这是 Array 的扩展,它定义了下一个版本的下标,这一次,如果您将索引命名为 extend:,则会使用达到所需索引的新值:

To demonstrate, here’s an extension to Array that defines another version of subscript, this time one that if you name the index extend:, will extend the array with the new value up to the desired index:

extension Array {
    // add a new subscript with a named argument
    subscript (extending index: Int) -> T {
        get { return self[index] }
        set(newValue) {
            if index < self.endIndex {
                self[index]
            }
            else {
                let extendBy = index + 1 - self.endIndex
                self.extend(Repeat(count: extendBy, repeatedValue: newValue))
            }
        }
    }
}

var a = [1,2,3]

a[extending: 5] = 100
// a is now [1, 2, 3, 100, 100, 100]

这篇关于下标语法可将值附加到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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