Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Basics Swift Operators Working With Operators

Getting an error line of code in challenge and in Xcode using % operator.

Line of code is:

let result: Double = value % divisor

Xcode says: " % is unavailable: Use truncatingRemainder instead" Challenge says: "Make sure result is assigned result of operation not simply assigned an Int value.

operators.swift
// Enter your code below
let value: Double = 200
let divisor: Double = 5

let someOperation = 20 + 400 % 10 / 2 - 15
let anotherOperation = 52 * 27 % 200 / 2 + 5

// Task 1 - Enter your code below
let result: Double = value % divisor

// Task 2 - Enter your code below
let isPerfectMultiple: Bool = result == 0

1 Answer

Hi Brian,

That error occurs because an expression using the %(modulo) operator returns an integer and in your answer value and divisor are defined as type Double. Swift is telling you to use the truncatingRemainder function because you will lose precision going from Double to Int. To fix the issue simply remove the type Double or change the type to Int for value and ** divisor** and it should work fine.

let value = 200
let divisor = 5

Hope this helps. ?

Thanks.