1

I'm working on an iOS application written in Objective C that has an attached class written in Swift. When I try to call a in my Obj C AppDelegate.m, I don't get any errors, but the reply callback never fires.

My ObjC has the following methods:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {

    NSString *success =[ApiController handleCall];
    //NSString *success =[self hello]; // This works if you swap it with the above method 
    //NSString *success = @"hello";    // when uncommented this also works.

    NSDictionary *dict = @{@"status" : success};

    reply(dict);
}

-(NSString *)hello{
    return @"hello";
}

NSString *success = [SwiftClass swiftMethod];

My swift class looks like this:

@objc class SwiftClass {
    @objc func swiftMethod()->NSString{
        return "it works!"
    }
}

In addition to the above code, I've made sure to include #import "ProjectName-Swift.h", as well as create a bridging header for the swift code.

Am I missing anything?

2
  • 1
    Your code isn't clear - Is your invocation of swiftMethod actually inside a method? Are you calling it on an instance of SwiftClass or are you literally calling [SwiftClass swiftMethod]? The latter won't work because swiftMethod isn't a type method. Commented Aug 4, 2015 at 1:35
  • 1
    "I don't get any errors, but code just." ??? Commented Aug 4, 2015 at 1:40

2 Answers 2

7

The swiftMethod is an instance method not a class method. You can call it like this:

NSString *success = [[SwiftClass new] swiftMethod];

If you want to call it as class method then you need to declare it with class:

@objc class SwiftClass {
    @objc class func swiftMethod()->NSString{
        return "it works!"
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Also you have to add @objc before class and method

@objc class MyClass: NSObjec {
   @objc func methodName() {
   }
}

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.