Swift 为什么我不能在函数中直接返回Void
在test1()中,它可以返回test()成功返回的Void。但在test2()中,会抛出错误。为什么?Swift 为什么我不能在函数中直接返回Void,swift,function,return,void,Swift,Function,Return,Void,在test1()中,它可以返回test()成功返回的Void。但在test2()中,会抛出错误。为什么? //: Playground - noun: a place where people can play import UIKit import AVFoundation func test()->Void{ print("Hello") } func test1(){//print Hello return test() } func test2(){// t
//: Playground - noun: a place where people can play
import UIKit
import AVFoundation
func test()->Void{
print("Hello")
}
func test1(){//print Hello
return test()
}
func test2(){// throw error
return Void
}
Void是一种类型,因此无法返回。相反,您希望返回Void的表示形式,它是一个空元组 因此,请尝试此方法,这样将编译:
func test()->Void{
print("Hello")
}
func test1(){//print Hello
return test()
}
func test2()->Void{// throw error
return ()
}
test1()
有关为什么可以在预期返回Void类型的函数中返回空元组的更多信息,请在以下链接中搜索Void:在test1()中,您没有返回Void,返回的是返回Void本身的函数test()
void function test(){
print("Hello");
}
void function test1(){
//print Hello
return test();
}
/* you can not return a type
func test2(){// throw error
return Void;
} */
void function test2(){
//code or not
return test(); //calling test function returns void.
}
我希望这会有帮助