I want to see how swift String struct pointer changes when I append new string to original one, comparing to Objective C. For Objective C I use this code:
NSMutableString *st1 = [[NSMutableString alloc] initWithString:@"123"];
[st1 appendString:@"456"];
In Objective C st1 string object changes internaly (adds 456 and becomes 123456), but st1 pointer lefts the same and points to the same object. In Swift, since String is not mutable, var st1 must change its address after addition, because it will hold another string, with my strings summ (123456). Is this all correct?
This is my playground code for swift tests:
import Cocoa
var str = "123"
withUnsafePointer(to: &str) { print($0) }
str += "456"
withUnsafePointer(to: &str) { print($0) }
var str2 = "123"
print(Unmanaged<AnyObject>.passUnretained(str2 as AnyObject).toOpaque())
str2 += "456"
print(Unmanaged<AnyObject>.passUnretained(str2 as AnyObject).toOpaque())
and this is results:
0x000000010e228c40 // this are same
0x000000010e228c40
0x00007fb26ed5a790 // this are not
0x00007fb26ed3f6d0
// and they completly different from first two
Why I get same pointer when I use withUnsafePointer? Why I get different pointers when I use Unmanaged.passUnretained? And why pointers received from this methods are completly different?