@objc protocol crashes the swift compiler

André Fratelli

I wrote I protocol which was intended to have some @optional methods, but the swift compiler crashes. This works:

protocol SessionDelegate {

    // TODO these should all be optional
    func willOpenSession(session: Session);
    func didOpenSession(session: Session);
    func didFailOpenningSession(session: Session, error: NSError!);

    func willCloseSession(session: Session);
    func didCloseSession(session: Session);
}

This doesn't:

@objc protocol SessionDelegate {

    @optional func willOpenSession(session: Session);
    @optional func didOpenSession(session: Session);
    @optional func didFailOpenningSession(session: Session, error: NSError!);

    @optional func willCloseSession(session: Session);
    @optional func didCloseSession(session: Session);
}

Honestly, having @objc is enough to crash the compiler. Is there any workaround?

Nate Cook

Right now your only way around this is to declare the protocol in an Objective-C header file and import the declaration via an Objective-C bridging header.

Protocol declaration:

// SessionDelegate.h

@class Session;

@protocol SessionDelegate <NSObject>

@optional

- (void)willOpenSession:(Session *)session;
- (void)didOpenSession:(Session *)session;
- (void)didFailOpenningSession:(Session *)session error:(NSError *)error;

- (void)willCloseSession:(Session *)session;
- (void)didCloseSession:(Session *)session;

@end

Bridging header:

// MyProject-Bridging-Header.h

#import "SessionDelegate.h"

Conforming class implementation in Swift:

// Session.swift

class Session {
    // ...
}

class MySessionDelegate: NSObject, SessionDelegate {
    func willOpenSession(session: Session) {
        // ...
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

@objc protocol crashes the swift compiler

From Dev

ObjC protocol Implementation in Swift

From Dev

Swift Compiler is attempting to compile ObjC

From Dev

Swift @objc protocol with optional variable CGFloat?

From Dev

Swift optional method in protocol without objc

From Dev

Swift 2 - @objc Protocol that throws an Error

From Dev

Swift optional method in protocol without objc

From Dev

Swift class "does not implement (objc) protocol"

From Dev

Swift @objc protocol with optional variable CGFloat?

From Dev

Optional Protocol methods in swift without using @objc

From Dev

Difference between @objc, @class_protocol and :class in swift Swift

From Dev

Swift compiler crashes on switch statement, need workaround

From Dev

Encoding an array of CLLocation in Swift crashes the compiler?

From Dev

Swift UIGestureEvent Crashes Without @objc Added Next To Selector

From Dev

iCarousel in swift version 1.2 error (candidate is not Objc but protocol requires it)

From Dev

Swift 2.2 #selector in protocol extension compiler error

From Dev

Objective-C calling parameterized Swift method crashes Swift compiler

From Dev

Candidate is not '@objc' but protocol requires it

From Java

Xcode 10.2 with Swift 5.0 compiler - protocol inheritance issue

From Java

Swift Compiler Error: "Type 'Watermark' does not conform to protocol 'ViewModifier'"

From Dev

Swift Class/Protocol is not recognised by Xcode compiler or Auto Complete

From Java

How can I make a weak protocol reference in 'pure' Swift (without @objc)

From Dev

What is the difference between declaring a protocol @objc and having it conform to NSObjectProtocol in pure Swift?

From Dev

Define method with closure in an @objc protocol

From Dev

Compiler crashes on generic lambda

From Java

When to use @objc in Swift?

From Dev

Use Swift Library in ObjC

From Dev

ObjC delegate callback in Swift

From Dev

Problems using ObjC in swift

Related Related

  1. 1

    @objc protocol crashes the swift compiler

  2. 2

    ObjC protocol Implementation in Swift

  3. 3

    Swift Compiler is attempting to compile ObjC

  4. 4

    Swift @objc protocol with optional variable CGFloat?

  5. 5

    Swift optional method in protocol without objc

  6. 6

    Swift 2 - @objc Protocol that throws an Error

  7. 7

    Swift optional method in protocol without objc

  8. 8

    Swift class "does not implement (objc) protocol"

  9. 9

    Swift @objc protocol with optional variable CGFloat?

  10. 10

    Optional Protocol methods in swift without using @objc

  11. 11

    Difference between @objc, @class_protocol and :class in swift Swift

  12. 12

    Swift compiler crashes on switch statement, need workaround

  13. 13

    Encoding an array of CLLocation in Swift crashes the compiler?

  14. 14

    Swift UIGestureEvent Crashes Without @objc Added Next To Selector

  15. 15

    iCarousel in swift version 1.2 error (candidate is not Objc but protocol requires it)

  16. 16

    Swift 2.2 #selector in protocol extension compiler error

  17. 17

    Objective-C calling parameterized Swift method crashes Swift compiler

  18. 18

    Candidate is not '@objc' but protocol requires it

  19. 19

    Xcode 10.2 with Swift 5.0 compiler - protocol inheritance issue

  20. 20

    Swift Compiler Error: "Type 'Watermark' does not conform to protocol 'ViewModifier'"

  21. 21

    Swift Class/Protocol is not recognised by Xcode compiler or Auto Complete

  22. 22

    How can I make a weak protocol reference in 'pure' Swift (without @objc)

  23. 23

    What is the difference between declaring a protocol @objc and having it conform to NSObjectProtocol in pure Swift?

  24. 24

    Define method with closure in an @objc protocol

  25. 25

    Compiler crashes on generic lambda

  26. 26

    When to use @objc in Swift?

  27. 27

    Use Swift Library in ObjC

  28. 28

    ObjC delegate callback in Swift

  29. 29

    Problems using ObjC in swift

HotTag

Archive