将字典从swift发送到objective-c类

时间:2016-02-11 11:24:48

标签: ios objective-c swift casting nsdictionary

我在快速项目中实现了CRToast。 CRToast是用Objective-c编写的。现在我想从swift文件中调用CRToast的方法,我需要将Dictionary作为参数发送。字典中包含不同类型的值。我怎样才能成功发送。

func showToast(){

     let options: [NSObject : AnyObject] = [kCRToastTextKey: "Hello World!",           
        kCRToastTextAlignmentKey: NSTextAlignmet.Center,
        kCRToastBackgroundColorKey: UIColor.redColor(),
        kCRToastAnimationInTypeKey: CRToastAnimationType.Gravity,
        kCRToastAnimationOutTypeKey: CRToastAnimationType.Gravity,
        kCRToastAnimationInDirectionKey: CRToastAnimationDirection.Left,
        kCRToastAnimationOutDirectionKey: CRToastAnimationDirection.Right
]

    CRToastManager.showNotificationWithOptions(options, completionBlock: {() -> Void in
        NSLog("Completed")
    })
}

在objective-c

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

我需要调用的方法是 showNotificationWithOptions

+ (void)showNotificationWithOptions:(NSDictionary*)options completionBlock:(void (^)(void))completion {
[self showNotificationWithOptions:options
                   apperanceBlock:nil
                  completionBlock:completion];

}

错误始终是get不能转换为期望的类型[NSObject:AnyObject]!。有人可以指导我如何发送具有不同值的词典

1 个答案:

答案 0 :(得分:2)

你应该传递枚举原始值:

func showToast(){

        let options: [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(options, completionBlock: {() -> Void in
            NSLog("Completed")
        })
    }