无夜之星辰
无夜之星辰

Reputation: 6158

Crashed when I call objective-c method in swift

Here is crash info: enter image description here

enter image description here

The LMAlertView is a three part use objective-c,I use it for a long time and everything goes well.But when I use it in swift,something strange occured:when I code init,Xcode alert for me and I just tap enter key so the whole func finished automatic.But the func the Xcode showed is not the real func in LMAlertView:

Here is the source code in LMAlertView:

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

As you see,argument otherButtonTitles is lose in my code which auto complete with Xcode.

If I add otherButtonTitles argument into my code,it will build failed.

How can I solve this issue?If anyone could shed some light, that would be wonderful.

Upvotes: 1

Views: 610

Answers (2)

Yun CHEN
Yun CHEN

Reputation: 6648

You need to change the source code of LMAlertView.

First, in LMAlertView.h, add a new init method with an array for other titles(don't forget to comment the old one):

//- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles;

Second, in LMAlertView.m, implement the new init method(don't forget to comment the old one):

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles {
    self = [super init];
    if (self) {
        _delegate = delegate;
        [self setupWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles];
    }
    return self;
}

Finally, you are able to call it in swift like this, without crashes:

let alertView = LMAlertView(title: "a title", message: "a message", delegate: self, cancelButtonTitle: "cancel", otherButtonTitles: ["other title1","other title2"])
//......

Upvotes: 1

Aravind A R
Aravind A R

Reputation: 2714

In Swift try like

let alertView = LMAlertView(title: "YourTitle",message:"your Message" delegate:nil cancelButtonTitle:"cancel" otherButtonTitles:nil)

Upvotes: 1

Related Questions