How do I get rid of "% is unavailable: Use truncatingRemainder instead"? [duplicate] - swift

This question already has answers here:
What does "% is unavailable: Use truncatingRemainder instead" mean?
(5 answers)
Closed 6 years ago.
I am new to coding (and this website) and I am trying to build a stopwatch using a tutorial off YouTube and I can't get it to work. When I program the function for updating the Timer, the error "% is unavailable: Use truncatingRemainder instead" shows on lines 6 and 7. I'm coding in the latest version of Swift using the latest Xcode version. Could someone please tell me how to get it to work with truncatingRemainder with milliseconds? I really appreciate it.
Here is the link to the tutorial: https://www.youtube.com/watch?v=xp0dvmvh3Cw
func updateElapsedTimeLabel (timer : Timer)
{
if watch.isRunning
{
let minutes = Int(watch.elapsd/60)
let seconds = Int(watch.elapsd % 60)
let ten0fseconds = Int(watch.elapsd * 10 % 10)
elapsedTimeLabel.text = String (format: "%2d:%2d.%2d", minutes, seconds, ten0fseconds)
}
else {
timer.invalidate()
}
}

just do the following...
let seconds = watch.elapsd.truncatingRemainder(dividingBy: 60)
let ten0fseconds = (watch.elapsd * 10).truncatingRemainder(dividingBy: 10)

Related

Removing "Optional" in Swift 4 [duplicate]

This question already has answers here:
How do I prevent my text from displaying Optional() in the Swift interpolation?
(3 answers)
Closed 5 years ago.
That's my code
let temperature = String(describing: Int(incomeTemp.text!))
celcjuszScore.text = "\(temperature)"
print(temperature)
Whent I am pushing a button, the result of print is "Optional(32)" (When I am writing 32 in incomeTemp). I would like to have "Optional" removed and only "32" should stay.
Just unwrap it.
if let temperature = Int(incomeTemp.text!) {
celcjuszScore.text = "\(temperature)"
print(temperature)
}
Remove the optional when converting text to number: Int(incomeTemp.text!) ?? 0.
Or solve the error explicitly:
if let temperature = Int(incomeTemp.text ?? "") {
celcjuszScore.text = "\(temperature)"
} else {
celcjuszScore.text = "Invalid temperature"
}

Unary operator '--' cannot be applied to an operand of type '#lvalue Int?' (aka '#lvalue Optional<Int>') [duplicate]

This question already has answers here:
The "++" and "--" operators have been deprecated Xcode 7.3
(12 answers)
Closed 6 years ago.
I just updated my app's code to the latest version of Swift and I have this function:
func setupGraphDisplay() {
//Use 7 days for graph - can use any number,
//but labels and sample data are set up for 7 days
//let noOfDays:Int = 7
//1 - replace last day with today's actual data
graphView.graphPoints[graphView.graphPoints.count-1] = counterView.counter
//2 - indicate that the graph needs to be redrawn
graphView.setNeedsDisplay()
maxLabel.text = "\((graphView.graphPoints).max()!)"
print((graphView.graphPoints).max()!)
//3 - calculate average from graphPoints
let average = graphView.graphPoints.reduce(0, +)
/ graphView.graphPoints.count
averageWaterDrunk.text = "\(average)"
//set up labels
//day of week labels are set up in storyboard with tags
//today is last day of the array need to go backwards
//4 - get today's day number
//let dateFormatter = NSDateFormatter()
let calendar = Calendar.current
let componentOptions:NSCalendar.Unit = .weekday
let components = (calendar as NSCalendar).components(componentOptions,
from: Date())
var weekday = components.weekday
let days = ["S", "S", "M", "T", "W", "T", "F"]
//5 - set up the day name labels with correct day
for i in (1...days.count).reversed() {
if let labelView = graphView.viewWithTag(i) as? UILabel {
if weekday == 7 {
weekday = 0
}
labelView.text = days[(weekday--)!]
if weekday! < 0 {
weekday = days.count - 1
}
}
}
}
However I am getting an error message at the following line:
labelView.text = days[(weekday--)!]
Where Xcode is giving me the following error:
Unary operator '--' cannot be applied to an operand of type '#lvalue Int?' (aka '#lvalue Optional<Int>')
I tried searching online for answers but I still couldn't find anything that would help me fix this error.
I was also curious what the error message exactly means by type #lvalue Int (aka '#lvalue Optional<Int>')? I have never seen this data type before and don't know how to solve the problem accordingly.
Thanks in advance.
Answer is very simple. ++ and -- was removed from Swift 3. But += and -= remained
About Optional<Int> this is longer version for Int? definition. In Swift Optional defined as
public enum Optional<Wrapped> : ExpressibleByNilLiteral

swift 2.2 basic, if statement doesn't compile [duplicate]

This question already has an answer here:
Error "{ expected after if statement"
(1 answer)
Closed 6 years ago.
Pardon me for beginner's question, Why this code got complained about { expected after if, the braces are already there
var years = Int(edtYears.text!)
if years !=nil {
//do something
}else {
//...
}
Thanks
You need to add space between both side of condition like if years != nil { or you can also write without space but the both side if years!=nil {
var years = Int("")
if years != nil {
//do something
}else {
//...
}
Never do like this. Make sure you do optional chaining otherwise you will surely get a crash.
if let text = edtYears.text, let convertToInt = Int(text){
print("Int \(convertToInt)")
}else{
print("Cannot convert")
}

swift iOS 7 replacement counter for time and distance

I have the following code in my running app
func eachSecond(timer: NSTimer) {
if deviceversion.floatValue >= 8.0 {
seconds++
let secondsQuantity = HKQuantity(unit: HKUnit.secondUnit(), doubleValue: seconds)
timeLabel.text = "Tijd: " + secondsQuantity.description
let distanceQuantity = HKQuantity(unit: HKUnit.meterUnit(), doubleValue: distance)
distanceLabel.text = "Afstand: " + distanceQuantity.description
let paceUnit = HKUnit.secondUnit().unitDividedByUnit(HKUnit.meterUnit())
let paceQuantity = HKQuantity(unit: paceUnit, doubleValue: seconds / distance)
paceLabel.text = "Snelheid: " + paceQuantity.description
}else{
}
}
This is using health kit which comes with ios 8. I also want people to use the app on their 7.1 iPhone. Is there an easy way to get the same information by using swift for iOS 7?
I can't seem to find how to do this and store this for later use. They made it real easy in iOS 8.
If you need to deploy your app on iOS releases before 8.0, you should perform the calculations directly rather than relying on HealthKit to provide the conveniences.

In swift, how do I do this? [duplicate]

This question already has answers here:
Adding Thousand Separator to Int in Swift
(7 answers)
Closed 7 years ago.
My English is not enough to search the question. So, I have to write here. My integer is 600000000 for ex. I want to convert it like this: 600,000,000. How do I do this?
extension Int {
struct Number {
static let formatter = NSNumberFormatter()
}
var addThousandSeparator:String {
Number.formatter.groupingSeparator = "."
Number.formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
return Number.formatter.stringFromNumber(self)!
}
}
let myInteger = 600000000
let myIntegerString = myInteger.addThousandSeparator // "600.000.000"