将字典从 swift 发送到 objective-c class
Send Dictionary from swift to objective-c class
我在我的 swift 项目中实现了 CRToast。 CRToast 写在Objective-c。现在我想从 swift 文件调用 CRToast 的方法,我需要为此发送字典作为参数。字典中包含不同类型的值。怎样才能发送成功呢
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 is can't cast to expected type [NSObject : AnyObject]!。有人可以指导我如何发送具有不同值的字典
您应该传递枚举原始值:
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")
})
}
我在我的 swift 项目中实现了 CRToast。 CRToast 写在Objective-c。现在我想从 swift 文件调用 CRToast 的方法,我需要为此发送字典作为参数。字典中包含不同类型的值。怎样才能发送成功呢
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 is can't cast to expected type [NSObject : AnyObject]!。有人可以指导我如何发送具有不同值的字典
您应该传递枚举原始值:
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")
})
}