加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 为什么我不能直接在函数中返回Void

发布时间:2020-12-14 05:32:06 所属栏目:百科 来源:网络整理
导读:在test1()中,它可以返回test()成功返回的Void.但是在test2()中,错误抛出.为什么? //: Playground - noun: a place where people can playimport UIKitimport AVFoundationfunc test()-Void{ print("Hello")}func test1(){//print Hello return test()}func
在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(){// 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:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读