0
votes

Im instantiating a mutable array, but it seems the elements become immutable so that I cant pass references to them to my function.

ERROR: "Cannot pass immutable value as inout argument: 'word' is a 'let' constant"

// MARK: - Properties

var wordsToTest = ["hannah", "bam"]

// MARK: -  Life cycle

override func viewDidLoad() {
    super.viewDidLoad()

    for word in wordsToTest {
        print("\(word) is a palindrome - ", isPalindrome(&word))
    }
}

How can I go about and fix it?

1
The real problem is the isPalindrome function. There is no reason why it should take an inout parameter. Change that function and don't worry about array being a constant. - Sulthan
Thanks. I have already solved the problem that way, and Im experimenting with different solutions for fun. isPalindrome is recursive and I did this to see if i could avoid instantiating new strings for each call. And this made me realize that i wouldnt modify the elements directly even if the array was a 'var'. And I wanted to learn more about it. So there is a reason, however how silly :P - Lord Fresh

1 Answers

0
votes

Simply use var in for loop:

for var word in wordsToTest //Here
{
    print("\(word) is a palindrome - ", isPalindrome(&word))
}