I am trying to shuffle an array of strings per day but I am encountering errors. One error is highlighted saying “ Cannot convert value of type '[String]' to expected argument type 'Range'” and another error is highlighted saying “ Initializer 'init(_:)' requires that 'Int.Words' conform to 'StringProtocol'”. I’m not sure what I am doing wrong. Please review my code below…
struct TestView: View {
var words: [String] = [
"Hello", "Hola", "Ciao",
]
var body: some View {
ZStack {
ForEach(isNewDay() ? words.shuffled() : words) { word in //Error: Cannot convert value of type '[String]' to expected argument type 'Range<Int>'
Text(word.words) //Error: Initializer 'init(_:)' requires that 'Int.Words' conform to 'StringProtocol'
.foregroundColor(Color.white)
.fontWeight(.bold)
.font(.title2)
.multilineTextAlignment(.center)
.padding(.horizontal, 15)
.padding(.top, 1)
}
}
}
func isNewDay()-> Bool{
let currentDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
let currentDateString = dateFormatter.string(from: currentDate)
if let lastSaved = UserDefaults.standard.string(forKey: "lastDate"){// last saved date
if lastSaved == currentDateString{
return true
}else{
UserDefaults.standard.setValue(currentDateString, forKey: "lastDate")
return false
}
}else{
UserDefaults.standard.setValue(currentDateString, forKey: "lastDate")
return false
}
}
}
CodePudding user response:
try this:
ForEach(isNewDay() ? words.shuffled() : words, id: \.self) { word in // <-- here
Text(word) // <-- here