0

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.

4
  • why you set 2 time return jsonSubString? Commented Jan 5, 2017 at 6:28
  • Sorry thats a typo, it actually should be parseString..i was doing some changes, forgot to change that Commented Jan 5, 2017 at 6:31
  • @AyushOjha What is the need of Second function here? Commented Jan 5, 2017 at 6:34
  • 3
    Please take the time and check that your code at least compiles before posting it (there is no void in Swift). Commented Jan 5, 2017 at 6:35

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.