Peterfei

上主是我的牧者,我实在一无所缺


  • 首页

  • 归档

  • 标签

swift中闭包使用

发表于 2016-09-04   |   分类于 Swift   |  
闭包是自包含的函数代码块,可以在代码中被传递和使用。
闭包可以捕获和存储其所在上下文中任意常量和变量的引用。这就是所谓的闭合并包裹着这些常量和变量,俗称 闭包。Swift 会为您管理在捕获过程中涉及到的所有内存操作。

Swift 的闭包表达式拥有简洁的风格,并鼓励在常见场景中进行语法优化,主要优化如下:

  1. 利用上下文推断参数和返回值类型
  2. 隐式返回单表达式闭包,即单表达式闭包可以省略 return 关键字
  3. 参数名称缩写
  4. 尾随(Trailing)闭包语法

##闭包表达式语法(Closure Expression Syntax)
闭包表达式语法有如下一般形式:

1
2
3
{ (parameters) -> returnType in
statements
}

demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@IBAction func operate(sender: UIButton) {
let operate = sender.currentTitle!
print("current operate is \(operate)")
switch operate {
case "+": performOperation({$0+$1})
case "−": performOperation({$1-$0})
case "×": performOperation({$0*$1})
case "÷": performOperation({$1/$0})
default:
break
}
}
func performOperation(operation:(Double, Double) -> Double) {
if operandStack.count>=2 {
displayValue = operation(operandStack.removeLast(),operandStack.removeLast())
print("displayValue is \(displayValue)")
enter()
}
}

Swift 基础语法

发表于 2016-08-20   |   分类于 Swift   |  

##声明常量和变量
let 声明的是常量 var 声明变量
e.g.

1
2
let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0

多个变量,分隔
var x = 0.0, y = 0.0, z = 0.0

##类型标注
var welcomeMessage: String

注: :后有空格

welcomeMessage变量现在可以被设置成任意字符串:welcomeMessage = "Hello"

常量一经声明就不允许改值,否则编译器报错

同ruby 语法类似 ,swift 在字符串中可这样输出变量||常量
将常量或变量名放入圆括号中,并在开括号前使用反斜杠将其转义

1
print("The current value of friendlyWelcome is \(friendlyWelcome)")

##属性观察器
属性观察器监控和响应属性值的变化,每次属性被设置值的时候都会调用属性观察器,甚至新的值和现在的值相同的时候也不例外
可以为属性添加如下的一个或全部观察器:

  1. willSet在新的值被设置之前调用
  2. didSet在新的值被设置之后立即调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class StepCounter {
var totalSteps: Int = 0 {
willSet(newTotalSteps) {
print("About to set totalSteps to \(newTotalSteps)")
}
didSet {
if totalSteps > oldValue {
print("Added \(totalSteps - oldValue) steps")
}
}
}
}
let stepCounter = StepCounter()
stepCounter.totalSteps = 200
// About to set totalSteps to 200
// Added 200 steps
stepCounter.totalSteps = 360
// About to set totalSteps to 360
// Added 160 steps
stepCounter.totalSteps = 896
// About to set totalSteps to 896
// Added 536 steps

##泛型函数

1
2
3
4
5
func swapTwoValues<T>(inout a: T, inout _ b: T) {
let temporaryA = a
a = b
b = temporaryA
}

rails page no cache

发表于 2016-08-16   |   分类于 ROR   |  

Rails 页面在完成权限时总是出现加载缓存页,导致加载出admin权限json渲染页。查看了浏览器请求,是页面cache引起,以下是解决方案:

1
2
3
4
5
6
7
before_filter :set_cache_headers
def set_cache_headers
response.headers["Cache-Control"] = "no-cache, no-store"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 2016 00:00:00 GMT"
end

ionic 加载app,页面空白解决方案

发表于 2016-05-21   |   分类于 Ionic   |  

给公司开发的app 经常会遇到打开加载空白页问题,虽然已经装了cordova.splashscreen.SplashScreen,但问题依然存在。
Google之后得到如下解决方案:

1
2
3
4
5
6
7
8
9
<preference name="AutoHideSplashScreen" value="false"/>
<preference name="ShowSplashScreenSpinner" value="false"/>
<preference name="SplashMaintainAspectRatio" value="true"/>
<preference name="SplashShowOnlyFirstTime" value="false"/>
<preference name="SplashScreenDelay" value="10000"/>
<preference name="FadeSplashScreen" value="false"/>
<feature name="SplashScreen">
<param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen"/>
</feature>

在app.js里加入:

1
2
3
4
5
6
7
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
setTimeout(function () {
navigator.splashscreen.hide();
}, 100);
}

Ruby 编程风格指南

发表于 2016-05-13   |   分类于 Ruby   |  

目录

  • 源代码排版
  • 语法
  • 命名
  • 注释
    • 注解
  • 类与模块
  • 异常
  • 集合
  • 数值
  • 字符串
  • 正则表达式
  • 百分号字面量
  • 元编程
  • 其他
  • 工具
阅读全文 »
1…678…16
peterfei

peterfei

peterfei|技术|上主是我的牧者

77 日志
14 分类
62 标签
RSS
github
© 2023 peterfei
由 Hexo 强力驱动
主题 - NexT.Mist