freew01f
freew01f

Reputation: 35

Swift call AnyClass class function

According to the article http://www.weheartswift.com/swift-objc-magic/ I create a NSObject extension to parse JSON string to object

import Foundation

extension NSObject {
    class func fromJson(jsonInfo: NSDictionary) -> Self {
        var object = self()

        (object as NSObject).load(jsonInfo)

        return object
    }

    func load(jsonInfo: NSDictionary) {
        for (key, value) in jsonInfo {
            let keyName = key as String

            if (respondsToSelector(NSSelectorFromString(keyName))) {
                setValue(value, forKey: keyName)
            }
        }
    }

    func propertyNames() -> [String] {
        var names: [String] = []
        var count: UInt32 = 0
        var properties = class_copyPropertyList(classForCoder, &count)
        for var i = 0; i < Int(count); ++i {
            let property: objc_property_t = properties[i]
            let name: String = String.fromCString(property_getName(property))!
            names.append(name)
        }
        free(properties)
        return names
    }

    func asJson() -> NSDictionary {
        var json:Dictionary<String, AnyObject> = [:]

        for name in propertyNames() {
            if let value: AnyObject = valueForKey(name) {
                json[name] = value
            }
        }


        return json
    }

}

I create a Class inherited NSObject

import Foundation

class Weather : NSObject {

    var date : String = ""
    var currentCity : String = ""
    var weather : String = ""
    var wind : String = ""
    var dayPictureUrl : String = ""
    var nightPictureUrl : String = ""
    var temperature : String = ""
}

then I build a helper to get API and parse result to Object

func requestApi(url :String, returnType: AnyClass, success: (res: AnyObject)->() ){
    var queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
    var group = dispatch_group_create()

    dispatch_group_async(group, queue) { () -> Void in
        var url = NSURL.init(string:url)
        var data = NSData.init(contentsOfURL:url!)

        let jsonObj: AnyObject? = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments,error:nil)

        if let topDict = jsonObj as? NSDictionary {
            var obj : AnyObject = returnType.fromJson(topDict)
            success(res: obj);
        }
    }    
}
var api : String = "http://somesite.com/jsonapi"

requestApi(api, Weather.self, { (res) -> () in
     // finish get the res obj
})

I know how to do it use Objective-C but Swift I get this error at this line

LINE : var obj : AnyObject = returnType.fromJson(topDict)
ERROR : 'AnyClass' does not h``ave a member named from JSON

I don't know a lot about Swift, I only want to call returnType class function , how can I do it ?

Upvotes: 1

Views: 2102

Answers (1)

qwerty_so
qwerty_so

Reputation: 36313

Your returnType is declared as being AnyClass. So of course it does not have a member fromJson. Probably you need to declare is as a class type that has this method.

Upvotes: 0

Related Questions