码迷,mamicode.com
首页 > 编程语言 > 详细

swift创建函数

时间:2015-07-09 18:00:06      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:ios   swift   


/*
        定义函数
    */
    //sayHello的函数主体首先定义了一个新的名为greeting的String常量
    
    //并将其设置加上personName组成一句简单的问候消息
    
    //(persnalName:String)函数的传参
    
    //String: 函数的返回值类型,返回箭头( - >)
    func sayHello(persnalName:String)->String
    {
        let greeting = "Hello" + persnalName + "!"
        return greeting;
    }
    
    //无返回值的函数
    func sayGoodBye(personNanem:String)
    {
        println("Goodbye,\(personNanem)!")
    }
    //第一个函数printAndCount,打印了一个字符串,然后并以Int类型返回它的字符数
    func printAndCount(stringToPoint:String)->Int
    {
         println(stringToPoint)
        return count(stringToPoint)
    }
    //第二个函数printWithoutCounting,调用的第一个函数,但忽略它的返回值
    //函数的调用
    //当第二函数被调用时,消息由第一函数打印了回来,但没有使用其返回值
    func printWithoutCounting(stringToPonit:String)
    {
        printAndCount(stringToPonit)
    }

func firstSwift(sender:UIButton)
    {
       println("第一个button的点击事件")
        
       self.label1.text = "button点击了"
    }


/*
            调用函数
        */
        println(sayHello("第一个函数"))
        sayGoodBye("first company")
        printAndCount("Hello world !")
        printWithoutCounting("hello world !")

版权声明:本文为博主原创文章,未经博主允许不得转载。

swift创建函数

标签:ios   swift   

原文地址:http://blog.csdn.net/darongzi1314/article/details/46819175

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!