Ios 在Watch App表中显示字符串数组时出现问题

Ios 在Watch App表中显示字符串数组时出现问题,ios,nsarray,nsuserdefaults,watchkit,apple-watch,Ios,Nsarray,Nsuserdefaults,Watchkit,Apple Watch,我在包含应用程序和watch应用程序之间传递字符串数组时遇到问题,我之前发布了一个关于接收错误的问题字符串与任何对象都不相同- 当我发布这个问题时,我是这样声明watch应用程序的数组的: var tempNames = [""] var tempAmounts = [""] var tempDates = [""] 现在我这样宣布: var tempNames = [] var tempAmounts = [] var tempDates = [] 这解决了另一个错误,但是我现在在另一行上

我在包含应用程序和watch应用程序之间传递字符串数组时遇到问题,我之前发布了一个关于接收错误的问题
字符串与任何对象都不相同
-

当我发布这个问题时,我是这样声明watch应用程序的数组的:

var tempNames = [""]
var tempAmounts = [""]
var tempDates = [""]
现在我这样宣布:

var tempNames = []
var tempAmounts = []
var tempDates = []
这解决了另一个错误,但是我现在在另一行上得到一个错误。现在,当我尝试在TableView中显示字符串时,我得到一个错误
“AnyObject”不能转换为“String”
。这是我的密码:

    for (index, tempName) in enumerate(tempNames) {
        let rowNames = recentsTable.rowControllerAtIndex(index) as RecentsTableRowController

        rowNames.nameLabel.setText(tempName)
    }

    for (index, tempAmount) in enumerate(tempAmounts) {
        let rowAmounts = recentsTable.rowControllerAtIndex(index) as RecentsTableRowController

        rowAmounts.amountLabel.setText(tempAmount)
    }

    for (index, tempDate) in enumerate(tempDates) {
        let rowDates = recentsTable.rowControllerAtIndex(index) as RecentsTableRowController

        rowDates.dateLabel.setText(tempDate)
    }
我在
rowNames.namelab.setText(tempName)
行中得到错误


哪里出错了?

在Swift中,数组总是包含显式类型的对象。。。与Objective-C不同,数组不能包含任意对象。因此,您需要声明数组将包含字符串

var tempNames = [String]()
var tempAmounts = [String]()
var tempDates = [String]()
这并不总是显而易见的,因为在某些工作代码中,您不会看到这一点。这是因为,如果编译器可以从上下文推断您正在数组中存储字符串,那么就可以忽略显式类型定义。例如:

var tempNames = ["Sarah", "Seraj", "Luther", "Aroha"]
关于上面的代码,您需要将
转换为?字符串

    for (index, tempName) in enumerate(tempNames) {
    let rowNames = recentsTable.rowControllerAtIndex(index) as RecentsTableRowController
    rowNames.nameLabel.setText(tempName) as? String
}

for (index, tempAmount) in enumerate(tempAmounts) {
    let rowAmounts = recentsTable.rowControllerAtIndex(index) as RecentsTableRowController
    rowAmounts.amountLabel.setText(tempAmount) as? String
}

for (index, tempDate) in enumerate(tempDates) {
    let rowDates = recentsTable.rowControllerAtIndex(index) as RecentsTableRowController
    rowDates.dateLabel.setText(tempDate) as? String
}

谢谢你的回复。当我尝试这样做时,我得到了将其更改为
[String]()
的建议,但当我进行更正时,错误
'String'与
AnyObject'不相同,再次出现在与以前相同的行上。更新为当前语法。在最初的帖子发布之前,我查看了Apple Swift手册以确认我所写的内容是正确的,但是在他们为Swift更改API时,
String[]()
已更改为
[String]()
。好的,使用您建议的代码,我现在得到的错误
'String'与
tempNames=defaults?.objectForKey(“namesWatch”)上的'AnyObject'
不相同,错误
'String'不是
rowNames.namelab.setText(tempName)as上的'Void'的子类型?字符串
行。有什么想法吗?