swift 闭包+嵌套函数+extension+单例+嵌套函数+??
//: Playground - noun: a place where people can play import UIKit //*******************嵌套函数*****************************
func getMathFunc(type:String) -> ((Int) -> Int) {
func squre(num:Int) -> Int{
return num * num
}
func cube(num:Int) -> Int{
return num * num * num
}
switch (type) {
case "squre":
return squre
default:
return cube
}
} var mathFunc = getMathFunc("squre")
mathFunc()
var mathFunc2 = getMathFunc("other")
mathFunc2() //********************闭包****************************
// { (形参列表) -> 返回值类型 in
// 可执行表达式
// } func getMathFunc1(type:String) -> ((Int) -> Int) {
func squre(num:Int) -> Int{
return num * num
}
func cube(num:Int) -> Int{
return num * num * num
}
switch (type) {
case "squre":
return {(num:Int) -> Int in
return num * num
}
default:
return {(num:Int) -> Int in
return num * num * num
}
}
}
var maxFunc3 = getMathFunc("squre")
maxFunc3()
var maxFunc4 = getMathFunc("other")
maxFunc4() //
var squre: (Int) -> Int = {(num) in return num * num}
squre() //?? 如果??左边有值就就是原值,如果没有值那么就设置为??右边的值
var a:Int?
//a = 11
print(a ?? )
//: Playground - noun: a place where people can play import UIKit
print("") // MARK - guard
func checkup(person:[String:String]){
guard let id = person["id"] else {
print("没有id,不能进入")
return
}
guard let exam = person["exam"] else{
print("没有exam,不能进入")
return
}
print("id:\(id),exam:\(exam)--批准进入")
}
//checkup(["id":"123"])
//checkup(["exam":"456"])
checkup(["id":"","exam":""]) //MARK - 熟悉观察
let MaxValue =
let MinValue = -
var number = {
willSet{
print("从\(number)变为\(newValue)")
}
didSet{
if number > MaxValue {
number = MaxValue
}else if number < MinValue{
number = MinValue
}
print("已经从\(oldValue)变为\(number)")
} }
number =
number //MARK - 扩展 extension
//对Int扩展,增加一个方法
extension Int {
func times(closure:(() -> ())?){
if self >= {
for _ in ..<self {
closure?()
}
}
}
}
.times{print("走起")}
//MARK 协议扩展
extension CustomStringConvertible{
var upperDescription:String{
return self.description.uppercaseString
}
}
["key":"value"].upperDescription //map:得到一个由闭包里面的返回值组成的新序列
//flatMap:与map类似的功能,但是会过滤掉返回值里面的nil值
//filter:得到一个由闭包返回值为true的值组成的新序列 var result = [,,,,].map{$ * }
result result = [,,,,].filter{$ > }
result //MARK 单例
class TestObject {
static let testObject = TestObject()
//私有构造,保证外部对象通过init方法创建单例类的其他实例
private init() { }
}
源码下载:https://github.com/pheromone/swift-extension-
http://download.csdn.net/detail/shaoting19910730/9515986
swift 闭包+嵌套函数+extension+单例+嵌套函数+??的更多相关文章
- IOS:利用dispatch_once创建单例
在之前有一篇学习笔记中,记载了一篇如何在OC中实现单例的文章:<IOS学习笔记4—Objective C—创建单例>自苹果引入了Grand Central Dispatch (GCD)(M ...
- Python函数式实现单例特性
传统的单例一般是基于类的特性实现,Python模块是天生的单例,下面来个简单的借助模块和函数实现单例特性: gdb = None def get_gdb(): global gdb if gdb is ...
- 再看 Java 中的单例
此前面试遇到了单例问题,本以为已经背的滚瓜烂熟,没想到被问单例如何避免被反射和序列化破坏,虽然后来还是等到了通知,但还是复习一下单例的实现方式,并学习防止反射和序列化破坏的手段. 基本实现方式 其他相 ...
- 10-Python入门学习-函数的对象与嵌套、名称空间与作用域、闭包函数
一.函数的对象 函数是第一类对象,指的是函数名指向的值(函数)可以被当作数据去使用 def func():# func=函数的内地址 print('from func') print(func) ag ...
- 苹果新的编程语言 Swift 语言进阶(六)--函数和闭包
一 .函数 1.1. 函数的定义和调用 函数的定义以funckeyword作为前缀,接着是函数名字,接着跟着一个能够带有參数.也能够不带參数的圆括号.接着用-> 指示函数的返回类型. 函数运行体 ...
- swift 学习(二)基础知识 (函数,闭包,ARC,柯里化,反射)
函数 func x(a:Int, b:Int) {} func x(a:Int, b:Int) -> Void {} func x(a:Int, b:Int) ->(Int,Int ...
- iOS——Swift开发中的单例设计模式(摘译,非原创)
最近在开发一个小的应用,遇到了一些Objective-c上面常用的单例模式,但是swift上面还是有一定区别的,反复倒来倒去发现不能按常理(正常的oc to swift的方式)出牌,因此搜索了一些帖子 ...
- JAVA之旅(十四)——静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制
JAVA之旅(十四)--静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制 JAVA之旅,一路有你,加油! 一.静态同步函数的锁是clas ...
- 在Swift中实现单例方法
在写Swift的单例方法之前可以温习一下Objective-C中单例的写法: + (instancetype)sharedSingleton{ static id instance; static d ...
随机推荐
- openCV1
openCV是一个提供有C++ , android , python的开源图像处理的类库 中文论坛的网址是http://www.opencv.org.cn/forum.php?mod=forumdis ...
- Servlet复习1: 一个简单的Servlet的使用
Servlet学习 1. Servlet与JSP的关系 2. Servlet的声明周期 3. 一个简单的Servlet的使用方法 什么是Servlet? 什么又是JSP? 继承了javax.servl ...
- 149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- json2.js 使用
参考:http://www.cnblogs.com/youring2/archive/2013/03/01/2938850.html github地址:https://github.com/dougl ...
- 知名黑客组织Anonymous(匿名者)的装备库
原文出处: infosecinstitute 译文出处:freebuf 欢迎分享原创到伯乐头条 本文关注的是世界著名的黑客组织Anonymous(匿名者).“我将描述他们的攻击方法和方式的计划 ...
- 《转》---使用递归方法DataTable 绑定 TreeView
转自:http://blog.sina.com.cn/s/blog_8944756d01016yaj.html 前台: <asp:View ID="view0" runat= ...
- Extjs 视频教程
---恢复内容开始--- 网易云课堂 <尚学堂_Ext视频教程> login.html <html> <head> <meta http-equiv=&quo ...
- IE9中Media queries在iframe无效的解决方法
在css中有5个media querie @media screen and(min-width:0px)and(max-width:319px){ body {background-color:re ...
- java日期的运用(DateUtils工具类)
public static void main(String[] args) { Date now = new Date(); SimpleDateFormat sd = new SimpleDate ...
- subString用法,字符串保持一定位数,不足补0
Substrinig(a,b): 从下标a开始截取,共截取b位 实现:一串数字,中间两位数字+2,生成新的一串数字 "; , number.Length - );//前8位 );//后6位 ...