0

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
    }
}

}

1 Answer 1

1

try this:

ForEach(isNewDay() ? words.shuffled() : words, id: \.self) { word in  // <-- here
                Text(word) // <-- here
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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