《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) {
}
}
相关文章推荐
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法
- Lua函数用法研究
- Lua基础教程之赋值语句、表达式、流程控制、函数学习笔记
- Sql Server中REPLACE函数的使用
- PowerShell函数简明教程