“String"与“AnyObject"错误不同 [英] 'String' is not identical to 'AnyObject' error

查看:42
本文介绍了“String"与“AnyObject"错误不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试通过应用程序中的 NSUserDefaults 将数据传递到我的 Apple Watch 应用程序时遇到问题.每当我尝试检索存储的数组时,都会收到错误 'String' 与 'AnyObject' 不同.

I'm having issues when trying to pass data to my Apple Watch app through NSUserDefaults from my app. Whenever I try to retrieve the array that is stored, I am getting the error 'String' is not identical to 'AnyObject'.

我一直在尝试找出解决方案,但我无法弄清楚问题出在哪里,因为我在我的应用程序的其他地方使用了相同的方法并且它没有问题.

I've been trying to figure out a solution but I can't work out what the issue is since I am using the same method elsewhere in my app and it works without issue.

这是我在 Apple Watch 部分的内容:

Here is what I have in the Apple Watch part:

    var defaults = NSUserDefaults(suiteName: "group.AffordIt")
    tempNames = defaults?.objectForKey("namesWatch") as NSArray
    tempDates = defaults?.objectForKey("datesWatch") as NSArray
    tempAmounts = defaults?.objectForKey("valuesWatch") as NSArray

以及包含的应用程序部分:

And the containing app part:

    defaults?.setObject(names, forKey: "namesWatch")
    defaults?.setObject(dates, forKey: "datesWatch")
    defaults?.setObject(values, forKey: "valuesWatch")

名称、日期和值是字符串数组.

names, dates and values are String arrays.

有什么想法吗?

推荐答案

let namesStringArray = ["John","Mary"]
let datesStringArray = ["May 21, 2009, 11:10 PM"]
let doublesStringArray = ["1.5","2.2","3.3"]

if let defaults = NSUserDefaults(suiteName: "group.AffordIt") {
    defaults.setObject(namesStringArray , forKey: "namesWatch")
    defaults.setObject(datesStringArray, forKey: "datesWatch")
    defaults.setObject(doublesStringArray, forKey: "valuesWatch")
}
let defaults = NSUserDefaults(suiteName: "group.AffordIt")
if let tempNames   = defaults!.stringArrayForKey("namesWatch") {
    println(tempNames)  // ["John", "Mary"]
}
if let tempDates   = defaults!.stringArrayForKey("datesWatch") {
    println(tempDates)  // ["May 21, 2009, 11:10 PM"]
}
if let tempAmounts = defaults!.stringArrayForKey("valuesWatch") {
    println(tempAmounts)  // ["1.5", "2.2", "3.3"]
}

这篇关于“String"与“AnyObject"错误不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆