The function below takes an array of User objects and returns the User object that has taken the most steps. The body of the function first declares a variable that is an optional User, then loops...


How do I fix this compiler error using Swift code?


The function below takes an array of User objects and returns the User object that has taken the most steps. The body of the function first declares a variable that is an optional User, then<br>loops through all of the users in the array. Inside each iteration of the loop, it will check if topCompetitor has a value or not by unwrapping it. If topCompetitor doesn't have a value,<br>then the current user in the iteration is assumed to have the highest score and is assigned to topCompetitor. If topCompetitor has a value, there is code to check whether the current<br>user in the iteration has taken more steps than the user that is assigned to topCompetitor.<br>At that point, the goal is to assign the user with the higher score to topCompetitor. However, the code generates a compiler error because, due to improper variable shadowing,<br>topCompetitor has a narrower scope than it should if it is going to be reassigned. Fix the compiler error below and call getWinner(competitors:), passing in the array<br>competitors. Print the name property of the returned User object. You'll know that you fixed the function properly if the user returned is activeSitter.<br>func getWinner(competitors: [User]) -> User? {<br>var topCompetitor: User?<br>for competitor in competitors {<br>if let topCompetitor<br>topCompetitor {<br>if competitor.stepsToday > topCompetitor.stepsToday {<br>= competitor<br>%3D<br>topCompetitor<br>Cannot assign to value: 'topCompetitor' is a 'let' constant<br>}<br>} else {<br>topCompetitor = competitor<br>}<br>}<br>return topCompetitor<br>}<br>

Extracted text: The function below takes an array of User objects and returns the User object that has taken the most steps. The body of the function first declares a variable that is an optional User, then loops through all of the users in the array. Inside each iteration of the loop, it will check if topCompetitor has a value or not by unwrapping it. If topCompetitor doesn't have a value, then the current user in the iteration is assumed to have the highest score and is assigned to topCompetitor. If topCompetitor has a value, there is code to check whether the current user in the iteration has taken more steps than the user that is assigned to topCompetitor. At that point, the goal is to assign the user with the higher score to topCompetitor. However, the code generates a compiler error because, due to improper variable shadowing, topCompetitor has a narrower scope than it should if it is going to be reassigned. Fix the compiler error below and call getWinner(competitors:), passing in the array competitors. Print the name property of the returned User object. You'll know that you fixed the function properly if the user returned is activeSitter. func getWinner(competitors: [User]) -> User? { var topCompetitor: User? for competitor in competitors { if let topCompetitor topCompetitor { if competitor.stepsToday > topCompetitor.stepsToday { = competitor %3D topCompetitor Cannot assign to value: 'topCompetitor' is a 'let' constant } } else { topCompetitor = competitor } } return topCompetitor }
Jun 10, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here