Issue with conforming to Objective-C protocol from Swift NSObject subclass

前端 未结 2 1127
野的像风
野的像风 2020-12-12 05:20

This piece of code works absolutely fine in Swift 1.1

// Obj-C

@import Foundation;

@protocol HashableObject 
- (NSUInteger)hash;
@end

//          


        
相关标签:
2条回答
  • 2020-12-12 06:16

    NSObject already has hash property:

    protocol NSObjectProtocol {
        var hash: Int { get }
    

    And, Swift 1.2 detects these erroneous overrides. From the release notes:

    Swift now detects discrepancies between overloading and overriding in the Swift type system and the effective behavior seen via the Objective-C runtime. (18391046, 18383574)

    For example, the following conflict between the Objective-C setter for “property” in a class and the method “setProperty” in its extension is now diagnosed:

    class A : NSObject {
      var property: String = "Hello" // note: Objective-C method 'setProperty:’
                                     // previously declared by setter for
                                     // 'property’ here
    }
    extension A {
        func setProperty(str: String) { } // error: method ‘setProperty’
                                          // redeclares Objective-C method
                                          //'setProperty:’
    }
    
    0 讨论(0)
  • 2020-12-12 06:16

    In Swift 1.2 (Xcode 6.3 beta 2) you can override the hash property of NSObject as a computed property:

    class Object: NSObject {
        override var hash: Int {
            return 0
        }
    }
    
    0 讨论(0)
提交回复
热议问题