Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift, Strings and Memory Addresses

There is something I am not understanding about how Swift manages memory address of String(s)

1. Reference types

Here foo and boo are 2 pointers to the same memory location.

class Foo { }

let foo = Foo()
let boo = foo

unsafeAddressOf(foo) // "UnsafePointer(0x7FCD13719BE0)"
unsafeAddressOf(boo) // "UnsafePointer(0x7FCD13719BE0)" 

Good.

2. Value types

let word0 = "hello"
let word1 = word0

Now word0 and word1 are value types but here the copy on write mechanism is involved.

[...] However, Swift only performs an actual copy behind the scenes when it is absolutely necessary to do so. Swift manages all value copying to ensure optimal performance, and you should not avoid assignment to try to preempt this optimization. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html#//apple_ref/doc/uid/TP40014097-CH13-XID_134

So why do they have 2 different memory addresses?

unsafeAddressOf(word0) // "UnsafePointer(0x7FCD1342ACE0)"
unsafeAddressOf(word1) // "UnsafePointer(0x7FCD13414260)"

3. More

Also please note that String is a struct that somehow conforms to AnyObject.

Tested with Xcode 7 GM Playground and Swift 2.0.

like image 904
Luca Angeletti Avatar asked Sep 17 '15 19:09

Luca Angeletti


People also ask

What is string interpolation in Swift?

String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. You can use string interpolation in both single-line and multiline string literals.

What does string mean in Swift?

A string is a series of characters, such as "Swift" , that forms a collection. Strings in Swift are Unicode correct and locale insensitive, and are designed to be efficient. The String type bridges with the Objective-C class NSString and offers interoperability with C functions that works with strings.

How do I count the number of characters in a string in Swift?

Swift – String Length/Count To get the length of a String in Swift, use count property of the string. count property is an integer value representing the number of characters in this string.

How do you print a string variable in Swift?

Just like variables and constants, you print a string by passing it to the print() function. Put this in your playground file and see what happens: print("I'm printing a string in Swift!")


1 Answers

func unsafeAddressOf(object: AnyObject) -> UnsafePointer<Void>

takes an AnyObject parameter, i.e. an instance of a class. It returns the pointer to the storage used for the object referenced by object.

addressOf() cannot be used with struct variables:

struct Foo { }
var f = Foo()
let a = unsafeAddressOf(f)
// error: cannot invoke 'unsafeAddressOf' with an argument list of type '(Foo)'

String is a struct, however, it is automatically bridged to NSString when passed to a function expecting an object. So

let word0 = "hello"
let p1 = unsafeAddressOf(word0)

actually executes

let p1 = unsafeAddressOf(word0 as NSString)

You get not the address of the word0 variable, but the pointer to the memory location of the bridged NSString object.

It seems that you cannot make any assumptions on whether this bridging returns the identical NSString object (or more generally, the same Foundation object) when done repeatedly on the same Swift string. In a Playground, even

let word0 = "hello"
let p1 = unsafeAddressOf(word0)
let p2 = unsafeAddressOf(word0)
let p3 = unsafeAddressOf(word0)

returns three different addresses (but the same addresses in a compiled project). The same observation (for arrays and dictionaries) was made in A different bridging between Array and Dictionary.

like image 159
Martin R Avatar answered Nov 23 '22 15:11

Martin R