ios - Playground crashes for Recursive Value Type -
xcode playground crashes code - if in project prevents compilation.
i trying declare simple struct:
struct node<t> { allow value: t var next: node<t>? init(_ value: t) { self.value = value next = nil } }
if in xcode playground next error message: the communication playground service interrupted unexpectedly.
if declare struct in separate file in xcode project cannot compiled. in case command failed due signal: segmentation fault: 11.
can help me this? there workaround? help much appreciated.
quoting swift documentation, "structures copied when passed around in code, , not utilize reference counting." [1]
a linked list trying accomplish works storing pointer or reference next node on current node. way, each node has same size. swift's struct on other hand not reference type. size of each node different depending on how many nodes has store recursively.
one way accomplish trying struct using unsafemutablepointer
. don't need warn because doing in swift makes write "unsafe" every few lines.
struct node<t> { var x: t // setting pointer type <node<t>> compiles, infinite loops on runtime (swift 1.2) var next: unsafemutablepointer<void> } var sec = node(x: 2, next: nil) var first = node(x: 1, next: &second) print(unsafebitcast(first.next, unsafemutablepointer<node<int>>.self).memory.x) // prints 2
[1] https://developer.apple.com/library/ios/documentation/swift/conceptual/swift_programming_language/classesandstructures.html
ios struct swift xcode6
No comments:
Post a Comment