Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override read-only property from Objective-C in Swift?

This is how it is defined in objective-c:

@interface AFHTTPRequestOperation : AFURLConnectionOperation

@property (readonly, nonatomic, strong) id responseObject;

How to assign sth to this property?

I would like to mock this property for later usage. So I need there my own responseObject overriden in Swift. Any ideas how to do this? I can subclass, extend or whatever. The only thing is it must by type of AFHTTPRequestOperation.

class MyRequestOperation: AFHTTPRequestOperation {

    override var responseObject: [String: AnyObject]
}

It produces an error:

Getter for responseObject with Objective-C selector responseObject conflicts with getter for responseObject from superclass AHTTPRequestOperation with the same Objective-C selector

like image 625
Bartłomiej Semańczyk Avatar asked Aug 06 '15 12:08

Bartłomiej Semańczyk


2 Answers

So, the answer is simple as @Atul Mishra said:

class MyRequestOperation: AFHTTPRequestOperation {

    var myOverridenResponseObject: AnyObject?

    override var responseObject: AnyObject? {

        get {
            return myOverridenResponseObject
        }

        set {
            myOverridenResponseObject = newValue
        }
    }
}
like image 63
Bartłomiej Semańczyk Avatar answered Nov 11 '22 05:11

Bartłomiej Semańczyk


You can present an inherited read-only property as a read-write property by providing both a getter and a setter in your subclass property override.but however you cannot provide an inherited read-write property as read-only property. for better clarification refer https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html#//apple_ref/doc/uid/TP40014097-CH17-XID_300

like image 7
Atul Mishra Avatar answered Nov 11 '22 04:11

Atul Mishra