简体   繁体   中英

I'm calling a Objective-C framework in swift, but it seems not working

I'm trying to use a Objective-C framework in my swift project. https://github.com/cruffenach/CRToast

Here is the example code

NSDictionary *options = @{
                          kCRToastTextKey : @"Hello World!",
                          kCRToastTextAlignmentKey : @(NSTextAlignmentCenter),
                          kCRToastBackgroundColorKey : [UIColor redColor],
                          kCRToastAnimationInTypeKey : @(CRToastAnimationTypeGravity),
                          kCRToastAnimationOutTypeKey : @(CRToastAnimationTypeGravity),
                          kCRToastAnimationInDirectionKey : @(CRToastAnimationDirectionLeft),
                          kCRToastAnimationOutDirectionKey : @(CRToastAnimationDirectionRight)
                          };
[CRToastManager showNotificationWithOptions:options
                            completionBlock:^{
                                NSLog(@"Completed");
                            }];

My codes look like this, it's not working with some errors

@IBAction func btnClicked(sender: AnyObject) {
    var option:Dictionary <NSObject,AnyObject> = 
       [kCRToastTextKey : "Hello World!",
        kCRToastTextAlignmentKey : NSTextAlignment.Center,
        kCRToastBackgroundColorKey : UIColor.whiteColor(),
        kCRToastAnimationInTypeKey : CRToastAnimationType.Gravity,
        kCRToastAnimationOutTypeKey : CRToastAnimationType.Gravity,
        kCRToastAnimationInDirectionKey : CRToastAnimationDirection.Left,
        kCRToastAnimationOutDirectionKey : CRToastAnimationDirection.Right]

    CRToastManager.showNotificationWithOptions(option, completionBlock: nil)
}

But when I just keep the 'ColorKey' , it will be fine

CRToastManager.showNotificationWithOptions([kCRToastBackgroundColorKey : UIColor.whiteColor()], completionBlock: nil)

Thanks a lot if u can help me with this problem

When using Objective-C framework in swift you need to add bridging header file.

See this link about bridging headers

I gave this a quick try, and the problem is with trying to stick Swift's enum values into the dictionary. If you grab the rawValue off each enum, it works for me:

var option: Dictionary<NSObject, AnyObject> = [
        kCRToastTextKey: "Hello World",
        kCRToastTextAlignmentKey: NSTextAlignment.Center.rawValue,
        kCRToastBackgroundColorKey: UIColor.redColor(),
        kCRToastAnimationInTypeKey: CRToastAnimationType.Gravity.rawValue,
        kCRToastAnimationOutTypeKey: CRToastAnimationType.Gravity.rawValue,
        kCRToastAnimationInDirectionKey : CRToastAnimationDirection.Left.rawValue,
        kCRToastAnimationOutDirectionKey : CRToastAnimationDirection.Right.rawValue
    ]

    CRToastManager.showNotificationWithOptions(option, completionBlock: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM