0

I have got an NSMutableArray declared like as:

var operatingDays = NSMutableArray()

And next i am appending the objects into the array like this :

for operatingDays in forecast.operatingDays! {
              print(operatingDays.days)
              print(operatingDays.times)
              self.operatingDays.add(operatingDays.days)
            }

So the operating days array now contains arrays.

Console Output

Now i want to convert each array to string separated with a space and to show it to the UITableViewCell. For this i am using the code at cellForRowAtIndexPath like this:

cell.operationalDays.text = (operatingDays[indexPath.row] as AnyObject).joined(separator: " ")

But it shows the error :

enter image description here

7
  • Cast to [String] instead of AnyObject, your array is an array of arrays? Commented Jul 20, 2017 at 12:07
  • @ReinierMelian does String have a method named joined? Commented Jul 20, 2017 at 12:08
  • yes of course it is Commented Jul 20, 2017 at 12:08
  • check my comment again, was edited @mag_zbc Commented Jul 20, 2017 at 12:08
  • @ChelseaShawra let me know if works Commented Jul 20, 2017 at 12:09

4 Answers 4

3

As I said in my first comment, casting your operatingDays[indexPath.row] to [String] it works, Try with this

if let arrayOfStrings = operatingDays[indexPath.row] as? [String]
{
  cell.operationalDays.text = arrayOfStrings.joined(separator: " ")
}

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

1

Solution is below:

var operatingDays = [Any]()
cell.operationalDays.text = (operatingDays[indexPath.row] as? [String])?.joined(separator: " ")

or

if let data = operatingDays[indexPath.row] as? [String] {
     cell.operationalDays.text = data.joined(separator: " ")
}

Comments

1

Try to joint array as below:

let tempArr = operatingDays[indexPath.row] as! NSArray
cell.operationalDays.text = tempArr.componentsJoined(by: " ")

Comments

0

Cast AnyObject to Array to use joined function.

if let isArray = operatingDays[indexPath.row] as? [String] {
    //Now you can able to use joined function
    cell.operationalDays.text = isArray.joined(separator: " ")
}

1 Comment

May i know the reason for down voting? is anything wrong let me know i will update myself.No use of down voting without commenting.Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.