【发布时间】:2019-01-02 18:15:09
【问题描述】:
上下文 - 我目前正在学习 Swift Struct。所以我决定在 Playground 中创建自己的电话结构。 (见下文)
问题 -
phone 结构上的 downloadApp 方法抛出以下错误.. Cannot use mutating member on immutable value: 'self' is immutable。
预期结果 - 和平地将新字符串附加到我的 apps 属性,这是一个字符串数组。
Swift 代码
struct Phone {
let capacity : Int
let Brand : Sting
var name: String
let model: String
var apps: [String]
var contacts: [String]
var formatCapacity: String {
return "\(capacity)GB"
}
func downloadApp(name: String){
apps.append(name) // ERROR
}
}
【问题讨论】:
标签: arrays swift struct append