1

I have an array A which contains 2 arrays :

[[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday], [Monday, Wednesday]]

Now i want to replace the elements of array A with:

[[Sun, Mon, Tue, Wed, Thu, Fri, Sat], [Mon, Wed]]

For this i am planning to use:

for days in operatingDays {
  for weekdays in days
  {
  if (operatingDays[i] as? String == "Sun") {
    operatingDays[i] = "Su"
  } else if operatingDays[i] as? String == "Mon"{
    operatingDays[i] = "Mo"
  } else if operatingDays[i] as? String == "Tue"{
    operatingDays[i] = "Tu"
  } else if operatingDays[i] as? String == "Wed"{
    operatingDays[i] = "We"
  } else if operatingDays[i] as? String == "Thu"{
    operatingDays[i] = "Th"
  } else if operatingDays[i] as? String == "Fri"{
    operatingDays[i] = "Fr"
  } else if operatingDays[i] as? String == "Sat"{
    operatingDays[i] = "Sa"
  }
}
}

But it is showing the error: enter image description here But how to implement this in Swift3 any idea will be appreciated.

4
  • Show us what did you try so far and what exactly doesn't work. Commented Jul 20, 2017 at 13:49
  • Its the same issue from your previous question you need to cast days to [String] Commented Jul 20, 2017 at 13:56
  • You need to tell the compiler that days is an Array and not a object of type ANy. Commented Jul 20, 2017 at 13:56
  • Why the long if-block? Just use substring to get the first three characters of the original string. Commented Jul 20, 2017 at 14:01

4 Answers 4

3
var input = [["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], ["Monday", "Wednesday"]]

var output = [[String]]()

for array in input {
    output.append(array.map {String($0.characters.prefix(3))})
}

the output will contain [["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], ["Mon", "Wed"]]

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

2 Comments

It is showing value of Type any has no member "Map"
You must specify that it is an array, for example with as? [String]
2

Cast operatingDays as something more specific

for days in operatingDays as! [[String]]

Comments

1

You can use the AnyObject type for casting your array, for example this code could be help you:

var array: [AnyObject] = [[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday], [Monday, Wednesday]]
array = [[Sun, Mon, Tue, Wed, Thu, Fri, Sat], [Mon, Wed]]

Now if you want to do something special, you must cast that array:

if let item = array[2] as? String{
    print(item)
}

Or you simply print your array:

for item in array{
    print("my item is \(item)")
}

Comments

1

Try with this, as I said you need to cast days as [String]

for days in operatingDays {
        if let daysArray = days as? [String]
        {
            for weekdays in daysArray
            {
               operatingDays[i] = 
                if (operatingDays[i] as? String == "Sun") {
                    operatingDays[i] = "Su"
                } else if operatingDays[i] as? String == "Mon"{
                    operatingDays[i] = "Mo"
                } else if operatingDays[i] as? String == "Tue"{
                    operatingDays[i] = "Tu"
                } else if operatingDays[i] as? String == "Wed"{
                    operatingDays[i] = "We"
                } else if operatingDays[i] as? String == "Thu"{
                    operatingDays[i] = "Th"
                } else if operatingDays[i] as? String == "Fri"{
                    operatingDays[i] = "Fr"
                } else if operatingDays[i] as? String == "Sat"{
                    operatingDays[i] = "Sa"
                }
            }
        }
    }

Hope this helps

1 Comment

@ChelseaShawra i am working in improving this code, i think with map can be solved in more shorty way as is showed in the accepted answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.