ash007
ash007

Reputation: 333

Return function from a function in Objective C?

I am using this swift function inside my objective c project to parse a JSON, this function looks like this: (This function is inside NetworkService class)

static func parseJSONString(jsonString: String?, key: String?) -> () -> String{
    var jsonSubString = String()
    func parseString() -> String{
        if let data = jsonString?.data(using: .utf8){
            if let content = try? JSONSerialization.jsonObject(with: data, options: []),
                let array = content as? [[String: AnyObject]]
            {

                for jsondict in array {
                    jsonSubString = jsondict[key!] as! String
                }
            }
        }
        return jsonSubString
    }
    return parseString
}

I want to call this function inside ViewController which is Objective C. It is a static method. Please help me in calling this function.

Upvotes: 0

Views: 1223

Answers (2)

Trond
Trond

Reputation: 221

You have to expose the function to Objective-C.

If your class is not already exposed to Objective-C you have to mark it with the @objc directive and inherit from NSObject, like this:

@objc class NetworkService: NSObject {

    static func parseJSONString(jsonString: String?, key: String?) -> () -> String{
        var jsonSubString = String()
        func parseString() -> String{
            if let data = jsonString?.data(using: .utf8){
                if let content = try? JSONSerialization.jsonObject(with: data, options: []),
                    let array = content as? [[String: AnyObject]]
                {

                    for jsondict in array {
                        jsonSubString = jsondict[key!] as! String
                    }
                }
            }
            return jsonSubString
        }
        return parseString
    }
}

Then in Objective-C you include the swift-generated header (I recommend including it in the .m-file), which is usually named [your-product-name-here]-Swift.h

You now should be able to call your function from Objective-C like this:

NSString* (^parseJson)(void) = [NetworkService parseJSONStringWithJsonString:@"SomeString" key:@"SomeOtherString"];
NSString* result = parseJson();

or simply like this:

NSString* otherResult = [NetworkService parseJSONStringWithJsonString:@"SomeString"
                                                                  key:@"SomeOtherString"]();

More on this in documentation from Apple here

Upvotes: 1

Satyanarayana
Satyanarayana

Reputation: 1067

Hi you wrote nested function means it will available within the scope of that function only, you need to call that nested function it is wirked as loop with condition , i given some example here once check

static func normalFunction(value: Int) -> Int {
    var valueTemp = value;
    func nestedFunction() -> Int {
        valueTemp = valueTemp * 20
        return valueTemp
    }
    if value % 2 == 0 {
        valueTemp = nestedFunction() //nested function calling 
    }
    return valueTemp
}

Upvotes: 0

Related Questions