+ 1

SWIFT Halloween Candy

Three nights with no sleep. Tried Classes, Structures and settled for normal functions... Now, if I don’t put the print line I don’t get an output... other combinations end up telling me that I can’t convert a String? to Int. I even tried to force the unwrap... I feel like I’m getting lost in something stupid. Help? ... ➡️ How do I tell the code to print the result? Thank youuu https://code.sololearn.com/cC60EIrRcy47/?ref=app Thank you 🙏🏻🙏🏻🙏🏻

1st Apr 2020, 3:52 PM
Nuno
7 Respuestas
+ 2
I don't know if you code will solve the challenge. But searching for swift codes on code playground gives me this solution: print(calc(houses: houses)) Your problem is now that var houses is a String. I think you need to convert it to Integer.
2nd Apr 2020, 11:08 AM
Denise Roßberg
Denise Roßberg - avatar
+ 2
let input = readLine() var num = Int(input!) print(num!) https://www.programiz.com/swift-programming/basic-input-output I am not familiar with swift so I don't know if this the only solution. Maybe it is better for to learn the basics of swift before you continue with this challenge.
3rd Apr 2020, 12:35 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
solved! it was all around the management of optionals
9th Apr 2020, 9:17 PM
Nuno
+ 1
You need to get some sleep.
23rd Dec 2020, 1:04 PM
Sonic
Sonic - avatar
0
tried that - it wont let me convert it even with an if let and adding ?? i swear it must be something silly im missing on...
3rd Apr 2020, 10:26 AM
Nuno
0
thanks denise, that article is how i did all my tries. surely the point of SoloLearn would be to be able to develop enough skills to be able to solve easy puzzles like these? anyway ive amended the code and i can confirm that it still does not allow me as it tells me i need to convert num to integer on line 4 func calc(number: Int) -> Int { let input = readLine() var num = Int(input!) var solution = 2 / num * 100 return solution } let input = readLine() var num = Int(input!) print(calc(number: num!)) if i make that same num as non-optional ! i get a fatal error... its just weird!
4th Apr 2020, 12:10 PM
Nuno
4th Apr 2020, 12:46 PM
Denise Roßberg
Denise Roßberg - avatar