Unable to use custom class in a protocol with @objc attribute?

后端 未结 4 1800
独厮守ぢ
独厮守ぢ 2021-01-04 11:55

I am trying to create a protocol for JSON loading delegation, JSONLoaderDelegate. My other class, called JSONLoader, is supposed to dispatch events

相关标签:
4条回答
  • 2021-01-04 12:15
    JSONLoaderError.LoadError
    

    Is this an enum type? You cannot convert Swift enums to Obj-C code. You'll need to use an int instead.

    0 讨论(0)
  • 2021-01-04 12:27

    I faced this problem as I had a class MyConnection that had no parent class. Like this.

    public class MyConnection {
    }
    

    I can't put @objc before this, as I get a warning

    Only classes that inherit from NSObject can be declared @objc

    So I changed the class to be inherited from NSObject. Like this

    public class MyConnection: NSObject {
    }
    
    0 讨论(0)
  • 2021-01-04 12:28

    What the 3 methods have in common is the JSONLoader parameter, and that's what I think prevents you from bridging the protocol. In order to solve the problem you have to make it objc compatible.

    0 讨论(0)
  • 2021-01-04 12:33

    I had the same error. method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C.

    I changed the syntax to the following in swift 2.0. It works fine now!

    @objc protocol AppModelDelegate {
        optional func compititorList(com:[Competitor]!)
    }
    
    class Competitor:NSObject{
        var id:Int?
        var title:String?
    }
    

    Note: the Competitor class type changed to NSObject which cleared the delegate rule violation

    Also in the case of NSJSON parsing. I changed to the following.

    if (_httpStatusCode == 200) {
        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        var error : NSError?
        print("responseString : \(responseString) ")
    
        var service_result:NSDictionary =  NSDictionary()
        do {
            let anyObj = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String:AnyObject]
            service_result = anyObj
        } catch let error as ErrorType {
            print("json error: \(error)")
        }
    }
    
    0 讨论(0)
提交回复
热议问题