[Solved] How I getting array data in Swift? [closed]


You need to be more precise when you write code. Your array of integers has a different number than the example output you want. I thought perhaps you wanted a set for a response, but there were two fails in there, so I ruled that out, too.

Another source of imprecision are the ranges you specified. You need to keep those tight so there’s no overlap. Without any further ado, you need to read up on map, filter, and reduce. For what I think you’re trying to do, you want to take a string and convert it to an array of grades based on specified ranges. For this, I would use map with a switch.

// Declare a var and use map to look at 
// each Int in the array and return a string
var grades = scores.map { score -> String in
    // each score goes through this switch and returns a String
    switch score {
    // 91 and greater returns "A"
    case 91...:
        return "A"
    // 80 to less than 91 returns "B"
    case 80..<91:
        return "B"
    // 70 to less than 80 returns "C"
    case 70..<80:
        return "C"
    // 60 to less than 70 returns "D"
    case 60..<70:
        return "D"
    // 0 to less than 60 returns "Fail"
    case 0..<60:
        return "Fail"
    // Everything else returns a blank string.
    default:
        return ""
    }
}


// ["Fail", "D", "C", "A", "Fail", "A", "B", "C", "D"]
print(grades)

Have a peek at UseYourLoaf.com’s post on map, filter, and reduce.

For the control flow, I used a switch statement. You can read more about them in Apple’s Documentation.

3

solved How I getting array data in Swift? [closed]