1

I've been having issues with passing an array of strings between a containing app and the watch app and I had posted a question earlier about receiving the error String is not identical to AnyObject - 'String' is not identical to 'AnyObject' error

When I posted that question I was declaring the watch app's arrays like so:

var tempNames = [""]
var tempAmounts = [""]
var tempDates = [""]

Now I am declaring them like this:

var tempNames = []
var tempAmounts = []
var tempDates = []

This resolves the other error, however I am now getting an error on a different line. Now when I try to display the strings in the TableView I get the error 'AnyObject' is not convertible to 'String'. Here is my code:

    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)
    }

I get the error on the rowNames.nameLabel.setText(tempName) line.

Where am I going wrong?

Community
  • 1
  • 1
user3746428
  • 11,047
  • 20
  • 81
  • 137

1 Answers1

0

In Swift, arrays always contain objects of an explicit type... unlike Objective-C the array can't contain arbitrary objects. You therefore need to declare that your arrays will contain strings.

var tempNames = [String]()
var tempAmounts = [String]()
var tempDates = [String]()

This isn't always immediately obvious, because in some working code you won't see this. That's because if the compiler can infer from the context that you are storing strings in the array, you are allowed to omit the explicit type definition. For example:

var tempNames = ["Sarah", "Seraj", "Luther", "Aroha"]

With regards to the code above, you need to cast as? String:

    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
}
Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
  • Thanks for the response. When I try that, I get the suggestion to change it to `[String]()` but when I make that correction, the error `'String' is not identical to `AnyObject'` occurs again on the same line as before. – user3746428 Jan 14 '15 at 23:58
  • Updated to current syntax. Had looked at the Apple Swift book to confirm what I was writing was correct before the original post, but `String[]()` has been changed to `[String]()` during their API changes for Swift. – Duncan Babbage Jan 15 '15 at 00:24
  • Ok, so using the code you suggested, I am now getting the error `'String' is not identical to 'AnyObject'` on the `tempNames = defaults?.objectForKey("namesWatch") as NSArray` lines, and the error `'String' is not a subtype of 'Void'` on the `rowNames.nameLabel.setText(tempName) as? String` lines. Any ideas? – user3746428 Jan 16 '15 at 14:21