无法将'[NSObject:AnyObject]'类型的值转换为预期的参数类型'[String:AnyObject]'

时间:2021-04-15 15:21:16

Xcode7 and swift, My code:

Xcode7和swift,我的代码:

func loadDefaults() {
    let settingBundle = NSBundle.mainBundle().pathForResource("Settings", ofType: "bundle")
    if settingBundle == nil {
        return
    }

    let root = NSDictionary(contentsOfFile: settingBundle!.stringByAppendingString("Root.plist"))

    let prefrences = root?.objectForKey("PreferenceSpecifiers") as! Array<NSDictionary>

    let defautlsToRegister = NSMutableDictionary(capacity: root!.count)

    for prefrence in prefrences {
        let key = prefrence.objectForKey("Key") as! String!
        if key != nil {
            defautlsToRegister.setValue(prefrence.objectForKey("DefaultVale"), forKey: key!)
        }
    }

    NSUserDefaults.standardUserDefaults().registerDefaults(defautlsToRegister as [NSObject: AnyObject])
}

Problem code:

问题代码:

NSUserDefaults.standardUserDefaults().registerDefaults(defautlsToRegister as [NSObject: AnyObject])

building warnings

建筑警告

Cannot convert value of type '[NSObject : AnyObject]' to expected argument type '[String : AnyObject]'

无法将'[NSObject:AnyObject]'类型的值转换为预期的参数类型'[String:AnyObject]'

change code:

改变代码:

NSUserDefaults.standardUserDefaults().registerDefaults(defautlsToRegister as [String: AnyObject])

building warnings

建筑警告

'NSMutableDictionary' is not convertible to '[String : AnyObject]'

'NSMutableDictionary'不能转换为'[String:AnyObject]'

Please teach me how to do? thanks.

请教我怎么做?谢谢。

3 个解决方案

#1


5  

Your defautlsToRegister should be in the following format [String: AnyObject]

您的defautlsToRegister应采用以下格式[String:AnyObject]

Example: The following should work without warning

示例:以下应该在没有警告的情况下工作

let defautlsToRegister = ["Test":10]
NSUserDefaults.standardUserDefaults().registerDefaults(defautlsToRegister as [String: AnyObject])

#2


2  

I've noticed a simple thing about this error. I'm not sure if this the case but casting String to NSString seems to solve the problem for me. I found an explanation that AnyObject is a type alias to represent instances of any reference type, which is for example: NSString. But String is a struct so it can't be the reference type for AnyObject.

我注意到这个错误很简单。我不确定是否这种情况但是将字符串转换为NSString似乎可以解决我的问题。我找到了一个解释,AnyObject是一个类型别名,用于表示任何引用类型的实例,例如:NSString。但String是一个结构,因此它不能是AnyObject的引用类型。

I see two ways for this:

我看到两种方式:

First:

第一:

let keyForMyKey: NSString = NSString(string: "mykey") let result = dict.objectForKey(keyForMyKey) as? NSMutableArray

let keyForMyKey:NSString = NSString(string:“mykey”)让result = dict.objectForKey(keyForMyKey)为? NSMutableArray里

Second:

第二:

let result = dict.objectForKey(NSString(string: "myKey")) as? NSMUtableArray

让result = dict.objectForKey(NSString(string:“myKey”))为? NSMutableArray里

More on the problem here: http://drewag.me/posts/swift-s-weird-handling-of-basic-value-types-and-anyobject

关于这个问题的更多信息:http://drewag.me/posts/swift-s-weird-handling-of-basic-value-types-and-anyobject

#3


0  

For me this is worked.

对我来说,这是有效的。

let token = fields["X-AUTH-TOKEN"]! as? [[String : AnyObject]] //{
                // safe to use employees
                self.bindings.setObject(NSString(format: "%@", token!) as String, forKey: "X-AUTH-TOKEN")

Format(NSString method) will work in this scenario.

格式(NSString方法)将在此方案中工作。

#1


5  

Your defautlsToRegister should be in the following format [String: AnyObject]

您的defautlsToRegister应采用以下格式[String:AnyObject]

Example: The following should work without warning

示例:以下应该在没有警告的情况下工作

let defautlsToRegister = ["Test":10]
NSUserDefaults.standardUserDefaults().registerDefaults(defautlsToRegister as [String: AnyObject])

#2


2  

I've noticed a simple thing about this error. I'm not sure if this the case but casting String to NSString seems to solve the problem for me. I found an explanation that AnyObject is a type alias to represent instances of any reference type, which is for example: NSString. But String is a struct so it can't be the reference type for AnyObject.

我注意到这个错误很简单。我不确定是否这种情况但是将字符串转换为NSString似乎可以解决我的问题。我找到了一个解释,AnyObject是一个类型别名,用于表示任何引用类型的实例,例如:NSString。但String是一个结构,因此它不能是AnyObject的引用类型。

I see two ways for this:

我看到两种方式:

First:

第一:

let keyForMyKey: NSString = NSString(string: "mykey") let result = dict.objectForKey(keyForMyKey) as? NSMutableArray

let keyForMyKey:NSString = NSString(string:“mykey”)让result = dict.objectForKey(keyForMyKey)为? NSMutableArray里

Second:

第二:

let result = dict.objectForKey(NSString(string: "myKey")) as? NSMUtableArray

让result = dict.objectForKey(NSString(string:“myKey”))为? NSMutableArray里

More on the problem here: http://drewag.me/posts/swift-s-weird-handling-of-basic-value-types-and-anyobject

关于这个问题的更多信息:http://drewag.me/posts/swift-s-weird-handling-of-basic-value-types-and-anyobject

#3


0  

For me this is worked.

对我来说,这是有效的。

let token = fields["X-AUTH-TOKEN"]! as? [[String : AnyObject]] //{
                // safe to use employees
                self.bindings.setObject(NSString(format: "%@", token!) as String, forKey: "X-AUTH-TOKEN")

Format(NSString method) will work in this scenario.

格式(NSString方法)将在此方案中工作。