gpt4 book ai didi

swift - 快速学习函数

转载 作者:搜寻专家 更新时间:2023-11-01 06:38:44 24 4
gpt4 key购买 nike

第一次来。我正在观看一个视频来补充我对 Swift 的学习(这似乎是一个较旧的视频,在调用时必须命名第一个 var,据我所知不再是这种情况)。无论如何,我很难理解第 7 行是怎么出错的。它说我不能让函数返回一个整数。它是函数中的函数,返回一个没有参数但返回整数类型的函数)。谢谢!

func buildIncrementor() -> () -> Int {
var count = 0
func incrementor() -> Int {
count += 1 //video uses ++count//
return count
}
return incrementor()
}
var incrementor = buildIncrementor()

最佳答案

试试这个

func buildIncrementor() -> () -> Int {
var count = 0
func incrementor() -> Int {
count += 1 //video uses ++count//
return count
}
return incrementor
}
var incrementor = buildIncrementor()

incrementor() 返回 incrementor() 函数的返回值,而 incrementor 返回函数。

关于swift - 快速学习函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38287320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com