您的位置:首页 > 移动开发 > Swift

《Swift 函数可以作为变量》

2016-02-15 11:41 483 查看


// 《函数变量》

//  ViewController.swift

//  Function_variables

/*

 *  函数可以存储在变量中。

 *  准备条件是:我们首先要声明一个变量,它能够存储一个接受特定参数、返回值的函数。

 *  条件是:一个函数的参数与返回值类型都与声明中的函数相同。

 */

//  Created by 周双建 on 16/2/14.

//  Copyright © 2016年周双建. All rights reserved.

//

import UIKit

class ViewController:
UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        // 添加主色

        self.view.backgroundColor =UIColor.whiteColor()

        //
创建导航标题

        self.view.addSubview(self.makeNav())

        //
进行下一步

        self.NextDo()

        //
函数的作为变量的嵌套

        let Love = 
self.GetResult(25, toAll:AddCount)

        print(Love)

        //
函数返回函数

        let GET = 
self.FunctionAddFunction(25)

        print(GET(25))

    }

    func makeNav() ->UIView {

        //
创建底层,对象

        let Nav_View =
UIView(frame: CGRectMake(0,0,self.view.bounds.width,64))

        // 添加颜色

        Nav_View.backgroundColor =
UIColor(colorLiteralRed: 222/255.0, green:222/255.0, blue:222/255.0,
alpha:1)

        //
添加标题,对象

        let Nav_title =
UILabel(frame: CGRectMake(Nav_View.center.x -100,20,200,40))

        //
设置显示模式

        Nav_title.textAlignment =NSTextAlignment.Center

        // 设置内容

        Nav_title.text =
"成功QQ吧"

        // 设置字体

        Nav_title.font =
UIFont.systemFontOfSize(22)

        // 设置颜色

        Nav_title.textColor =UIColor.blackColor().colorWithAlphaComponent(0.8)

        //
将其添加到第一个对象上

        Nav_View.addSubview(Nav_title)

        return Nav_View

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    //
举个简单的演示

   
// 我先创建一个加法函数

    func AddNumbers (firstNumber numOne :
Int , SecondNumber numTwo :Int ) ->
Int {

        return numOne + numTwo

    }

   
// 我们在这里进行演示(函数变量的使用)

    func NextDo() {

       
// 我们现在创建一个变量 (我们要保持类型相同,同时参数量也的相同)

        var FunctionVariables:(
Int,Int) ->Int

        //
满足上面的条件,我们就可以把 AddNumbers
函数存入FunctionVariables变量里面

        FunctionVariables = AddNumbers

        //
我们进行使用 (我们用FunctionVariables这个变量求 100 + 150的值)

        print(FunctionVariables(150 ,100))

        

       let  Result:String =String(stringInterpolationSegment: FunctionVariables(100,150))

        

       self.AlertViewFunction(Result, Target:self)

        

    }

   
// 我们在写一个提示函数(我们要传入提示内容)

    func AlertViewFunction(Message :
String, Target : UIViewController) {

        // 提示主体

        let Alert =
UIAlertController(title: "温馨提示", message: Message, preferredStyle:UIAlertControllerStyle.Alert)

        // 按钮模块

        let ActionCancel =
UIAlertAction(title: "不要", style:UIAlertActionStyle.Cancel)
{ (UIAlertAction) ->Void
in

            print("你好狠")

        }

        let ActionGet =
UIAlertAction(title: "送给你", style:UIAlertActionStyle.Default)
{ (UIAlertAction) ->Void
in

            print("你的到了")

        }

        //
按钮的装载

        Alert.addAction(ActionCancel)

        Alert.addAction(ActionGet)

        //
显示       

       
// 做延时操作,防止视图层次混乱

        dispatch_after(3,dispatch_get_main_queue()) { () ->
Voidin

           Target.presentViewController(Alert, animated:true) { () ->
Voidin

           }

        }

    }

    /******************函数作为变量的嵌套***********************/

    //
乘法函数

    func AddCount(number:Int) ->Int{

        print("Success")

        return number *
4;

    }

    //
最后结果函数

    func GetResult(Zsj_number:
Int ,toAll: (Int)->Int)->Int{

        return toAll(Zsj_number)

    }

    /********************************************************/

    

    /******************函数作为返回值*************************/

    func FunctionAddFunction(JoinNumber:Int)->(Int)->Int{

        func Jion(_:Int)->Int{

            return JoinNumber *
25

        }

        return Jion

    }

     

    /********************************************************/

    override func viewDidAppear(animated:Bool) {

    }

    

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息