Swift - Code using closure to find sum of two numbers with the given code

//Write a program using two Int numbers(num1 and num2) and returns sum of those numbers//

given and disabled code:

import Foundation//given and disabled

// Write your code here.(I have to write my code here so that it gives 10 is 5 5 is given as input at run time)

 //my code 
let closure: (Int,Int) -> (Int) = {
return $0 + $1
}

//below code is given code and disabled.

let fileName = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: fileName, contents: nil, attributes: nil)

let fileHandle = FileHandle(forWritingAtPath: fileName)!
guard let number1 = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)

else { fatalError("Bad input") }

guard let number2 = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)

else { fatalError("Bad input") }

let res = let closure(number1: number1, number2: number2)

fileHandle.write(String(res).data(using: .utf8)!)

fileHandle.write("\n".data(using: .utf8)!)

now when I write my code it is giving compile error in disabled code . Please help.

1 answer

  • answered 2018-05-16 05:59 Kamran

    You should use closure like this,

    let res = closure(number1, number2)