Search code examples
swiftrx-swiftmoya

Instance method requires that 'NSObject' conform


I extended Observable of RxSwift and I am using it with Moya so I wrote it out like this

/// JSON map to object
    /// - returns: Observable<T>
    func jsonMapObject<T: EVReflectable>(type: T.Type, forKeyPath: String? = nil) -> Observable<Result<T, ORMError>>  {

        return self.map { rawResult in

            guard let result = rawResult as? Result<JSON, ORMError> else {
                return .failure(ORMError.ORMParseJSONError)
            }

            switch result {
            case let .success(json):

                guard json != JSON.null,
                    let dict = json.dictionaryObject else {
                        return .failure(ORMError.ORMParseJSONError)
                }

                guard let object: T = self.dictMap(from: dict) as T else {
                    return .failure(ORMError.ORMCouldNotMakeObjectError)
                }

                return .success(object)

            case let .failure(error):
                return .failure(error)
            }

        }
    }

after writing this code as such, I get an error in the editor saying

Instance method 'dictMap(from:forKeyPath:)' requires that 'NSObject' conform to 'EVReflectable'

I do not know how to proceed again as this error does not go away

my dictMap is written as

///

Create the object from the dictionary
    internal func dictMap<T: NSObject>(from: NSDictionary?, forKeyPath: String? = nil) -> T where T: EVReflectable{
        let instance: T = T()
        let parsedObject: T = ((instance.getSpecificType(from ?? NSDictionary()) as? T) ?? instance)
        let _ = EVReflection.setPropertiesfromDictionary(from ?? NSDictionary(), anyObject: parsedObject, forKeyPath: forKeyPath)
//        if self.statusCode > 300  {
//            instance.addStatusMessage(DeserializationStatus.Custom, message: "HTTP status code: \(self.statusCode)")
//        }
        return parsedObject
    }

Solution

  • Your function func dictMap<T: NSObject>(from: NSDictionary?, forKeyPath: String? = nil) -> T where T: EVReflectable Requires T to be an NSObject and conform to EVReflectable. In order to do that, NSObject must conform to EVReflectable.

    You likely just need to remove : NSObject> from the above:

    func dictMap<T>(from: NSDictionary?, forKeyPath: String? = nil) -> T where T: EVReflectable