-2

Sorry i know it must be easy to solve that but i have no idea how. Could anyone help me?enter image description here

//Gesamtpausen
var pauses:[Double]=[]
for learnunit in learnunits {
    pauses.append(learnunit.totalPauseTime!)
}
//GesamtLernzeit
var studying:[Double]=[]
for learnunit in learnunits {
    studying.append(learnunit.totalStudyingTime!)
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 2
    your totalPauseTime is nil – Shehata Gamal Jan 27 '18 at 16:18
  • 1
    You should always post the code not a screenshot, and your are trying to unwrap something that is nil or empty – Lamour Jan 27 '18 at 16:21
  • is there any option to solve that? i mean i know that some older data has the totalPauseTime nil. anyways i want to add up the ones that are not nil – Mauritius Julius Joseph Klein Jan 27 '18 at 16:27
  • var pauses: [Double] = [] var studying: [Double] = [] learnUnits.forEach { (learnunit) in if let pauseTime = learnunit.totalPauseTime { pauses.append(pauseTime) } if let studyingTime = learnunit.totalStudyingTime { studying.append(studyingTime) } } – Sandeep Jan 27 '18 at 16:44
  • You can simplify this to one line of each: `let totalPause = learnUnits.flatMap { $0.totalPauseTime }.reduce(0.0, +)`. The `flatMap` unwraps the values, discarding those that are nil. The `reduce(0.0, +)` adds them up for you. – Rob Jan 27 '18 at 17:05

1 Answers1

0

try this:

if let pauseTime = learnunit.totalPauseTime {
    pauses.append(pauseTime)
}
congsun
  • 144
  • 5