Swift:使用闭包添加2个数字的程序

时间:2018-05-04 22:04:08

标签: swift

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)!)

这是一个问题。想要在res中添加和,以便可以打印res。我是斯威夫特的新人。请帮助。在不修改问题的情况下编写的额外代码是什么?

尝试了以下解决方案:

if let res = closure(number1: number1, number2: number2){
    return number1+number2
}

func closure(number1: Int, number2: Int) -> Int {
    return number1 + number2
}

获得以下错误:

solution.swift:23:11: error: expected initial value after '='
let res = let closure(number1: number1, number2: number2)
          ^
solution.swift:23:10: error: consecutive statements on a line must be separated by ';'
let res = let closure(number1: number1, number2: number2)
         ^
         ;
solution.swift:23:22: error: consecutive statements on a line must be separated by ';'
let res = let closure(number1: number1, number2: number2)
                     ^
                     ;
solution.swift:23:15: error: type annotation missing in pattern
let res = let closure(number1: number1, number2: number2)
              ^
solution.swift:23:22: warning: expression of type '(number1: Int, number2: Int)' is unused
let res = let closure(number1: number1, number2: number2)

2 个答案:

答案 0 :(得分:2)

Swift中的闭包非常简单。这是一个用于添加两个数字的示例Closure:

let closure:((Int, Int) -> Int) = { (number1, number2) in
    return number1 + number2
}

与普通变量一样,您可以在冒号后设置闭包的类型。在这种情况下,它是:

((Int, Int) -> Int)

表示:将两个Int作为参数并返回Int。

用法:

let firstNumber = 5
let secondNumber = 6

let additionResult = closure(firstNumber, secondNumber)
//additionResult is 11

在您的具体用例中:

let closure:((Int, Int) -> Int) = { (number1, number2) in
    return number1 + number2
}

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 = closure(number1, number2)

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

答案 1 :(得分:1)

  

闭包是自包含的功能块,可以在代码中传递和使用。 Swift中的闭包类似于C和Objective-C中的块以及其他编程语言中的lambdas。

     

闭包可以捕获和存储对定义它们的上下文中的任何常量和变量的引用。这被称为关闭那些常量和变量。 Swift为您处理捕获的所有内存管理。

     

参考:click here

示例:

对于排序,您可以定义一个简单的函数,并将其作为参数传递给sorted(by:)方法:

let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]

func backward(_ s1: String, _ s2: String) -> Bool {
    return s1 > s2
}

var reversedNames = names.sorted(by: backward)
// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]

您可以为此编写闭包,而不是使用函数,如:

var reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in
    return s1 > s2
})

解决您的问题:

你写作和使用:

func closure(number1: Int, number2: Int) {
    return number1 + number2
}

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

或者,您可以实施:

let closure:((Int, Int) -> Int) = { (number1, number2) in return number1 + number2 }

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