1

I have the following If-Statment and I was wondering how this could be achieved with a switch statement?

I am trying to represent an integer value within an array as a string (e.g. 1 == "Jan")

func assigningMonthName([Data]) {
    for i in dataset.arrayOfDataStructures {
        if (i.month) == 1 {
            println("Jan")
        }
        else if (i.month) == 2 {
            print("Feb")
        }
        else if (i.month) == 3 {
            print("March")
        }
        else if (i.month) == 4 {
            print("April")
        }
        else if (i.month) == 5 {
            print("May")
        }
        else if (i.month) == 6 {
            print("June")
        }
        else if (i.month) == 7 {
            print("July")
        }
        else if (i.month) == 8 {
            print("August")
        }
        else if (i.month) == 9 {
            print("September")
        }
        else if (i.month) == 10 {
            print("October")
        }
        else if (i.month) == 11 {
            print("November")
        }
        else if (i.month) == 12 {
            print("December")
        }
        else {
            println("Error assigning month name")
        }
    }

}

any answers would be appreciated :)

1
  • Just a suggestion, find a way to get month from Int by using NSDate. It will make your life easier than use if else or switch case Commented Oct 19, 2015 at 8:22

2 Answers 2

2

While you can use a switch, this is essentially just another way to write if-else so there is no big improvement to your code:

switch i.month {
    case 1:
        print("Jan")
    case 2:
        print("Feb")
    ...
}

What about using an array?

let monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "Sept", "October", "November", "December"]
print(monthNames[i.month - 1])

The system actually already contains month names, they are even localized:

let monthNames = NSDateFormatter().monthSymbols;
print(monthNames[i.month - 1])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! this makes a lot of sense. I have created an empty string within the arrayofdatastructures called 'monthValue', if i wanted to insert the written month. I tried 'i.monthName.insert(monthNames[i.month - 1])' however i got the error 'immutable value of type string only has mutating members named insert'
@JessMurray That's a bit different question. Probably you want to create a new string (e.g. using String(format: ...) and assign that instead of modyfing an already existing string.
1

Try this:

switch i.month {
    case 1:
        print("Jan")
    case 2:
        print("Feb")
    ...
    default:
        print("default value")
}

Comments

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.