I have written the following code as part of Swift bootcamp challenge. This code runs perfectly on Replit but when I run the code on the playground in Xcode 13.3.1 I get a huge block of error. Please help me figure out whats the error about.
var aYear = Int(readLine()!)!
func isLeap(year: Int) {
if year % 4 == 0 && year % 400 == 0 && year % 100 == 0 {
print("Yes")
} else {
print("No")
}
}
isLeap(year: aYear)
Here's the huge error message:
__lldb_expr_13/leapyYearusingIfElse.playground:25: Fatal error: Unexpectedly found nil while unwrapping an Optional value Playground execution failed:
error: Execution was interrupted, reason: EXC_BREAKPOINT (code=1, subcode=0x18f6dd21c). The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in Swift._assertionFailure(_: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 356 frame #1: 0x000000018f6dcf84 libswiftCore.dylib
closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in Swift.assertionFailure(: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 192
frame #2: 0x000000018f6dc954 libswiftCore.dylibSwift._assertionFailure(_: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never + 228 frame #3: 0x0000000100510480 $__lldb_expr14
main at leapyYearusingIfElse.playground:0
frame #4: 0x00000001000d33c8 leapyYearusingIfElselinkResources + 272 frame #5: 0x0000000180360580 CoreFoundation
CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK + 20
frame #6: 0x000000018035f854 CoreFoundation__CFRunLoopDoBlocks + 408 frame #7: 0x000000018035a018 CoreFoundation
__CFRunLoopRun + 764
frame #8: 0x0000000180359804 CoreFoundationCFRunLoopRunSpecific + 572 frame #9: 0x000000018c23660c GraphicsServices
GSEventRunModal + 160
frame #10: 0x0000000184d7bd2c UIKitCore-[UIApplication _run] + 992 frame #11: 0x0000000184d808c8 UIKitCore
UIApplicationMain + 112
frame #12: 0x00000001000d3488 leapyYearusingIfElsemain + 192 frame #13: 0x00000001003b9cd8 dyld_sim
start_sim + 20
frame #14: 0x0000000100309088 dyld`start + 516Your code cannot run on the playground even though it can run on repl environment because readLine() returns nil.
Your code attempts to unwrap the nil value forcibly. You should give up operating the code involving readLine() on the playground.