Re: [swift-users] access violation when weak variable
You code works fine in Xcode 7.3.1 (7D1014). So it must be a bug in Xcode 8. Zhaoxin On Sat, Jul 9, 2016 at 10:18 PM, Ray Fix via swift-users < swift-users@swift.org> wrote: > > Hi! > > When I make a variable weak in Xcode 8 (both beta 1 and beta 2) I get a > access violation. I think this is a bug, but want to make sure I am not > missing something. > > Best regards, > Ray > > //: Playground - noun: a place where people can play > > import UIKit > > class Person: CustomStringConvertible { > var name: String > weak var parent: Person? /// If I remove weak, no crash in Xcode 8 > beta 2, but leaks > var children: [Person] = [] { > didSet { > children.forEach { $0.parent = self } > } > } > > init(name: String) { > self.name = name > print("initialized \(name)") > } > deinit { > print("deinit \(name)") > } > var description: String { > return name > } > } > > do { > let frank = Person(name: "Frank") > let lisa = Person(name: "Lisa") > frank.children = [lisa] /// KABOOM! > } > > ___ > swift-users mailing list > swift-users@swift.org > https://lists.swift.org/mailman/listinfo/swift-users > ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
Re: [swift-users] object.self?
Yes, as far as I know 'foo' and 'foo.self' are equivalent. I don't actually know why the latter exists, except in analogy to "T.self". There was a mistake in my response; the metatype of 'foo' is not 'foo.self', it is 'foo.dynamicType' (or whatever new form dynamicType is going to take in Swift 3). On Sat, Jul 9, 2016 at 2:27 PM, Rick Mannwrote: > > > On Jul 8, 2016, at 09:45 , Austin Zheng wrote: > > > > Hi Rick, > > > > If you have a type (let's call it "T"), you can use it two ways: > > > > * As a type, or part of a type, like such: "let x : T = blah()" > > * As a value, just like any other variable, function argument, property, > etc. > > > > In the second case (type-as-value), you need to append ".self" to the > type name according to the grammar: > > > > "let x : Any.Type = T.self" > > > > There was a "bug" in Swift 2.x where you could sometimes use just "T", > without the ".self", in certain cases (in particular, when you were passing > in a type-as-value to a function with one unlabeled argument). That bug has > since been fixed. > > > > As for types-as-values: Swift allows you to treat a type as a normal > value, which means you can do whatever you want with it: pass it to > functions and return it from functions, store it in properties or > variables, etc. If you have one of these types-as-values (called > 'metatypes'), you can do certain things like call static methods or > initializers on them, use them to parameterize generic functions, etc. > > Thanks, Austin. I'm familiar with all this in Swift. What threw me was > that "subclassObject" was an instance, not a class. > > > However, to get back to your original question, the `.self` in that > switch statement actually isn't necessary and you should really just be > switching on the value of subclassObject itself, not the value of its type. > > I would have thought so, but the response to this answer was something > along the lines of "I never know when to use the object or its self." > > To me, for an instance, foo an foo.self should be equivalent in all > respects (shouldn't it?). > > > > > Best, > > Austin > > > > > > On Fri, Jul 8, 2016 at 9:38 AM, Rick Mann via swift-users < > swift-users@swift.org> wrote: > > I just saw a question which brought up something I didn't know about. > Apparently sometimes you have to call object.self in a place that looks > like you should just use "object." What does this usage mean? > > > > for subclassObject in objects { > > switch subclassObject.self {<--- Here, why not > "subclassObject" alone? > > case is Subclass1: > > doSomethingWith(subclassObject as! Subclass1) > > > > case is Subclass2: > > doSomethingWith(subclassObject as! Subclass2) > > > > case is Subclass3: > > doSomethingWith(subclassObject as! Subclass3) > > > > default: > > break > > } > > } > > > > Thanks, > > Rick > > > > > On Jul 8, 2016, at 08:15 , Dan Loewenherz via swift-users < > swift-users@swift.org> wrote: > > > > > > To my knowledge, you can’t do exactly what you’re trying to do, but > this is close: > > > > > > for subclassObject in objects { > > > switch subclassObject.self { > > > case is Subclass1: > > > doSomethingWith(subclassObject as! Subclass1) > > > > > > case is Subclass2: > > > doSomethingWith(subclassObject as! Subclass2) > > > > > > case is Subclass3: > > > doSomethingWith(subclassObject as! Subclass3) > > > > > > default: > > > break > > > } > > > } > > > > > > On Fri, Jul 8, 2016 at 10:11 AM, Nate Birkholz via swift-users < > swift-users@swift.org> wrote: > > > This looks like it doesn't work (swift 2.x), but wanted to be sure > it's not supported: > > > class Superclass {} > > > class Subclass1 : Superclass {} > > > class Subclass2 : Superclass {} > > > class Subclass3 : Superclass {} > > > > > > let sc1 = Subclass1() > > > let sc2 = Subclass2() > > > let sc3 = Subclass3() > > > > > > let objects : [Superclass] = [sc1, sc2, sc3] > > > > > > for subclassObject in objects { > > > switch subclassObject { > > > case let object = subclassObject as? Subclass1: > > > doSomethingWith(object) > > > case let object = subclassObject as? Subclass2: > > > doSomethingWith(object) > > > case let object = subclassObject as? Subclass3: > > > doSomethingWith(object) > > > default: > > > return > > > } > > > } > > > > > > This gives an error, expecting a colon (:) after object on every case. > > > > > > I wanted to be sure I wasn't missing something in my syntax (nor some > obvious-to-others reason this isn't supported) before going to swift > evolution. > > > > > > > > > -- > > > Nate Birkholz > > > > > > ___ > > > swift-users mailing list > > > swift-users@swift.org > > > https://lists.swift.org/mailman/listinfo/swift-users > > > > > > > > >
Re: [swift-users] object.self?
> On Jul 8, 2016, at 09:45 , Austin Zhengwrote: > > Hi Rick, > > If you have a type (let's call it "T"), you can use it two ways: > > * As a type, or part of a type, like such: "let x : T = blah()" > * As a value, just like any other variable, function argument, property, etc. > > In the second case (type-as-value), you need to append ".self" to the type > name according to the grammar: > > "let x : Any.Type = T.self" > > There was a "bug" in Swift 2.x where you could sometimes use just "T", > without the ".self", in certain cases (in particular, when you were passing > in a type-as-value to a function with one unlabeled argument). That bug has > since been fixed. > > As for types-as-values: Swift allows you to treat a type as a normal value, > which means you can do whatever you want with it: pass it to functions and > return it from functions, store it in properties or variables, etc. If you > have one of these types-as-values (called 'metatypes'), you can do certain > things like call static methods or initializers on them, use them to > parameterize generic functions, etc. Thanks, Austin. I'm familiar with all this in Swift. What threw me was that "subclassObject" was an instance, not a class. > However, to get back to your original question, the `.self` in that switch > statement actually isn't necessary and you should really just be switching on > the value of subclassObject itself, not the value of its type. I would have thought so, but the response to this answer was something along the lines of "I never know when to use the object or its self." To me, for an instance, foo an foo.self should be equivalent in all respects (shouldn't it?). > > Best, > Austin > > > On Fri, Jul 8, 2016 at 9:38 AM, Rick Mann via swift-users > wrote: > I just saw a question which brought up something I didn't know about. > Apparently sometimes you have to call object.self in a place that looks like > you should just use "object." What does this usage mean? > > for subclassObject in objects { > switch subclassObject.self {<--- Here, why not "subclassObject" > alone? > case is Subclass1: > doSomethingWith(subclassObject as! Subclass1) > > case is Subclass2: > doSomethingWith(subclassObject as! Subclass2) > > case is Subclass3: > doSomethingWith(subclassObject as! Subclass3) > > default: > break > } > } > > Thanks, > Rick > > > On Jul 8, 2016, at 08:15 , Dan Loewenherz via swift-users > > wrote: > > > > To my knowledge, you can’t do exactly what you’re trying to do, but this is > > close: > > > > for subclassObject in objects { > > switch subclassObject.self { > > case is Subclass1: > > doSomethingWith(subclassObject as! Subclass1) > > > > case is Subclass2: > > doSomethingWith(subclassObject as! Subclass2) > > > > case is Subclass3: > > doSomethingWith(subclassObject as! Subclass3) > > > > default: > > break > > } > > } > > > > On Fri, Jul 8, 2016 at 10:11 AM, Nate Birkholz via swift-users > > wrote: > > This looks like it doesn't work (swift 2.x), but wanted to be sure it's not > > supported: > > class Superclass {} > > class Subclass1 : Superclass {} > > class Subclass2 : Superclass {} > > class Subclass3 : Superclass {} > > > > let sc1 = Subclass1() > > let sc2 = Subclass2() > > let sc3 = Subclass3() > > > > let objects : [Superclass] = [sc1, sc2, sc3] > > > > for subclassObject in objects { > > switch subclassObject { > > case let object = subclassObject as? Subclass1: > > doSomethingWith(object) > > case let object = subclassObject as? Subclass2: > > doSomethingWith(object) > > case let object = subclassObject as? Subclass3: > > doSomethingWith(object) > > default: > > return > > } > > } > > > > This gives an error, expecting a colon (:) after object on every case. > > > > I wanted to be sure I wasn't missing something in my syntax (nor some > > obvious-to-others reason this isn't supported) before going to swift > > evolution. > > > > > > -- > > Nate Birkholz > > > > ___ > > swift-users mailing list > > swift-users@swift.org > > https://lists.swift.org/mailman/listinfo/swift-users > > > > > > ___ > > swift-users mailing list > > swift-users@swift.org > > https://lists.swift.org/mailman/listinfo/swift-users > > > -- > Rick Mann > rm...@latencyzero.com > > > ___ > swift-users mailing list > swift-users@swift.org > https://lists.swift.org/mailman/listinfo/swift-users > -- Rick Mann rm...@latencyzero.com ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
Re: [swift-users] access violation when weak variable
The actual death is happening when setting the parent: do { let frank = Person(name: "Frank") print("frank\(frank)") let lisa = Person(name: "Lisa") frank.parent = lisa // Dies here with EXC_BAD_ACCESS You can click the eye to get a stack trace. (not included here because of rdar://27263098 - can't get a copyable stack trace from a hard error in playgrounds). My bet it's something playground related - about 9 frames down in the stack trace has a reference to the Playground Logger doing the Mirror thing. Putting your code in to a standalone swift executable lets it work. Cheers, ++md On Sat, Jul 9, 2016 at 10:18 AM, Ray Fix via swift-users < swift-users@swift.org> wrote: > > Hi! > > When I make a variable weak in Xcode 8 (both beta 1 and beta 2) I get a > access violation. I think this is a bug, but want to make sure I am not > missing something. > > Best regards, > Ray > > //: Playground - noun: a place where people can play > > import UIKit > > class Person: CustomStringConvertible { > var name: String > weak var parent: Person? /// If I remove weak, no crash in Xcode 8 > beta 2, but leaks > var children: [Person] = [] { > didSet { > children.forEach { $0.parent = self } > } > } > > init(name: String) { > self.name = name > print("initialized \(name)") > } > deinit { > print("deinit \(name)") > } > var description: String { > return name > } > } > > do { > let frank = Person(name: "Frank") > let lisa = Person(name: "Lisa") > frank.children = [lisa] /// KABOOM! > } > > ___ > swift-users mailing list > swift-users@swift.org > https://lists.swift.org/mailman/listinfo/swift-users > ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
[swift-users] access violation when weak variable
Hi! When I make a variable weak in Xcode 8 (both beta 1 and beta 2) I get a access violation. I think this is a bug, but want to make sure I am not missing something. Best regards, Ray //: Playground - noun: a place where people can play import UIKit class Person: CustomStringConvertible { var name: String weak var parent: Person? /// If I remove weak, no crash in Xcode 8 beta 2, but leaks var children: [Person] = [] { didSet { children.forEach { $0.parent = self } } } init(name: String) { self.name = name print("initialized \(name)") } deinit { print("deinit \(name)") } var description: String { return name } } do { let frank = Person(name: "Frank") let lisa = Person(name: "Lisa") frank.children = [lisa] /// KABOOM! } ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users