问题
Start with the claim that Swift makes that strings are 'mutable', Are Swift "mutable" strings really mutable, or are they just like Java strings?, but proceeding with a generally-accepted (and non-Swift) definition of mutability - ie. strictly value immutability, without consideration of bindings
Is it possible to actually mutate a String value such that this prints 'true'?
var str = "Mutate me!"
let a1 = (unsafeAddressOf(str))
// some 'mutating operation'
let a2 = (unsafeAddressOf(str))
print(a1 == a2)
I am not interested in 'structure types' or delayed copy semantics. This question is about if a string value can be modified, from within Swift - although it would be also be interesting to see if such could be mutated by other .. devious means. (If not it is hogwash to even bother discussing value vs reference types instead of pure mutability considerations.)
I am aware of "mutating" methods but disagree with those as 'mutating' the string value. In the example above, let
should be substitutable in the final program. If let
cannot be substituted; or the object can otherwise not be proven to be the same, then mutability has not been shown.
来源:https://stackoverflow.com/questions/32637688/is-it-possible-to-mutate-a-string-in-swift-such-that-it-can-be-proven-to-modify