我有一个关于数组切片的问题。我对数组切片的理解是,它不使用内存的另一部分,而是使用原始数组。但是我不明白在我们选择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
定义内存,或者只有当我们将slice
和array
定义为时,内存策略才会选择slice
?