选择数组切片时修改原始数组

时间:2018-07-04 08:20:42

标签: arrays swift

我有一个关于数组切片的问题。我对数组切片的理解是,它不使用内存的另一部分,而是使用原始数组。但是我不明白在我们选择slice或slice数组的范围内修改原始数组时会发生什么。例如以下代码的输出

fetch(restUrl, {
                method: "POST",
                headers: {
                    'x-nanobnk-apikey': urls.APIKey,
                    // 'x-nanobnk-auth': this.token,
                    'Accept-Language': this.state.stLang,
                    'Accept': '*/*',
                    'charset': 'utf-8',
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(objReq)
            })
        )
            // .then(ApiUtils.checkStatus)
            .then(response => {

                if (response.status >= 200 && response.status < 300) {
                    return response.text()
                }
                else {
                    return response.json()
                }
            }
            )
            .then(responseJson => {
                let obj = responseJson;
                if (responseJson != null && responseJson != undefined) {
                    if (obj.hasOwnProperty('error') || obj.hasOwnProperty('errorType')) {
                        // failure scenario
                        console.log(responseJson + "from failure scenario");
                        this.setState({ btnDisableBool: false });
                        alert(obj.message);

                    }
                    else {
                        // Success response

                        }

                }
                else {
                    this.setState({ btnDisableBool: false });
                    alert(i18n.t('unable'));
                }
            })

var array = [1,2,3,4,5,6]
var slice = array[1..<3]

array[1] = 5
print(array)
print(slice)

我的问题是,当我们修改[1, 5, 3, 4, 5, 6] [2, 3] 时,swift将复制数组并为array定义内存,或者只有当我们将slicearray定义为时,内存策略才会选择slice

0 个答案:

没有答案
相关问题