[swift-users] Protocol composition results in EXC_BAD_ACCESS

Adrian Zubarev adrian.zubarev at devandartist.com
Mon Apr 3 09:48:34 CDT 2017


Why do you cast against an existential in first place?

This solves the given example:

protocol Something {}

protocol IntContainer {
    var ints: [Int] { get set }
}

func processSomething(_ state: Something) {

    if let intContainer = state as? IntContainer {
        print(intContainer.ints)
    }
}

struct MySomething: Something, IntContainer {
    var ints: [Int] = []
}

let mySomething = MySomething()
processSomething(mySomething)  


-- 
Adrian Zubarev
Sent with Airmail

Am 3. April 2017 um 16:31:02, Rudolf Adamkovič via swift-users (swift-users at swift.org) schrieb:

I'm getting an `EXC_BAD_ACCESS` here:

protocol Something {}

protocol IntContainer {
    var ints: [Int] { get set }
}

func processSomething(_ state: Something) {
    if let intContainer = state as? Something & IntContainer {
        print(intContainer.ints)
    }
}

struct MySomething: Something, IntContainer {
    var ints: [Int] = []
}

let mySomething = MySomething()
processSomething(mySomething) // EXC_BAD_ACCESS here

The execution stops here:

libswiftCore.dylib`_swift_release_dealloc:
    0x11be163e0 <+0>: movq   (%rdi), %rax
->  0x11be163e3 <+3>: jmpq   *-0x10(%rax)
    0x11be163e6 <+6>: nopw   %cs:(%rax,%rax)

Any ideas why this is?

P.S. Related StackOverflow question with no good answer:
http://stackoverflow.com/questions/35199077/exc-bad-access-using-protocol-composition

R+
_______________________________________________
swift-users mailing list
swift-users at swift.org
https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170403/ef231cc2/attachment.html>


More information about the swift-users mailing list