简体   繁体   中英

Convert Objective-C dictionary definition to Swift

I'm using the CRToast class for an app and in the documentation, it says to declare the options as:

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

I've mostly converted the code to swift, except for the first key-value pair. I'm getting a syntax error that says "Type 'NSString!' does not conform to protocol 'Hashable'". My swift translation is:

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

UPDATE: I tried adding a line of code above the options declaration to make an NSString

let text:NSString = "Hello World!"

And then used

var options = [kCRToastTextKey :text,

The hashable message went away, now I'm getting "'NSString' is not convertible to 'CRToastAnimationDirection'"

I think the kCRToastKeys are not compliant to being hashable types,

try this:

var options = [kCRToastTextKey as String):"Hello World!",
        kCRToastTextAlignmentKey as String: NSTextAlignment.Center,
        kCRToastBackgroundColorKey as String : UIColor.redColor(),
        kCRToastAnimationInTypeKey as String : CRToastAnimationType.Gravity,
        kCRToastAnimationOutTypeKey as String : CRToastAnimationType.Gravity,
        kCRToastAnimationInDirectionKey as String : CRToastAnimationDirection.Left,
        kCRToastAnimationOutDirectionKey as String : CRToastAnimationDirection.Right]

You can use rawValue like this answer.

https://stackoverflow.com/a/27034350

Should be something like this.

let options:[NSObject:AnyObject] = [
    kCRToastTextKey: "Hi!",
    kCRToastTextAlignmentKey: NSTextAlignment.Center.rawValue,
    kCRToastBackgroundColorKey: UIColor.blueColor(),
    kCRToastAnimationInTypeKey: CRToastAnimationType.Gravity.rawValue,
    kCRToastAnimationOutTypeKey: CRToastAnimationType.Gravity.rawValue,
    kCRToastAnimationInDirectionKey: CRToastAnimationDirection.Left.rawValue,
    kCRToastAnimationOutDirectionKey: CRToastAnimationDirection.Right.rawValue
]

This was answered on github here . Use:

var options: NSDictionary = [
    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
]

Same poster I believe. The answer was only slightly different than ckang's.

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