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

Swift 函数的基本写法与使用元组实现返回多个值(四)

发布时间:2020-12-14 07:09:56 所属栏目:百科 来源:网络整理
导读:无参数无返回值 let studentScores = [ 12 , 55 , 65 , 38 , 99 , 88 , 0 ] func studySwift () - Void { print ( "无参无返回值" ) } 无参数有返回值 func studySwift1 () - String { let name :String = "无参数有返回值" return name } 有参数无返回值 fu

无参数无返回值

let studentScores = [12,55,65,38,99,88,0]
func studySwift() ->Void
    {
        print("无参无返回值")
    }

无参数有返回值

func studySwift1() ->String
    {
        let name:String = "无参数有返回值"
        return name
    }

有参数无返回值

func sayHi (name:String) ->Void
    {
        print("Hi,(name)")
    }

有参数有返回值

func sayHello (name:String)->String
    {
        let result = "Hello," + name
        return result
    }

使用元组返回多个值

func maxminScores ( scores:[Int] ) -> (maxscore:Int,minscore:Int)
    {
        var curmax = scores[0],curmin = scores[0]
        for score in scores[1..<scores.count]
        {
            curmax = max(curmax,score)
            curmin = min(curmin,score)
        }
        return (curmax,curmin)
    }

调用

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.

        // "!"表示这个可选变量存在,可以使用,如果用"!"访问不存在的可选变量会导致一些错误

        // "?"表示这个变量可能不存在,如果不存在,"?"所在语句后面的内容都不会执行

        // !是一个强制拆包,告诉编译器我绝对肯定代码能够执行,如: strValue!.hashValue,如果不能执行则报错。
        // ?是表示一个不确定,strValue?.hashValue 就等于OC的if(strValue){  [strValue hashValue]; } 有就执行,有没后面代码就不执行。 不会报错。
关于 ? ! 的解释转自:        [http://blog.csdn.net/wmqi10/article/details/37562071](http://blog.csdn.net/wmqi10/article/details/37562071)


        let name:String = "zjw"
        print(sayHello(name))
        sayHi(name)
        studySwift()
        print(studySwift1())
        let result = maxminScores(studentScores)
        print("max:(result.maxscore),min:(result.minscore)")
    }

(编辑:李大同)

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

    推荐文章
      热点阅读