[转]Swift Cheat Sheet
原文:http://kpbp.github.io/swiftcheatsheet/
A quick cheat sheet and reference guide for Apple's Swift language. This guide intends to cover all the key features of Swift, including Strings, Arrays, Dictionaries and Flow Control.
Swift is a new programming language for developing iOS and OS X apps that was introduced by Apple in June 2014.
Variables
var myInt = 1
var myExplicitInt: Int = 1 // explicit type
var x = 1, y = 2, z = 3 // declare multiple integers
myExplicitInt = 2 // set to another integer value
Constants
let myInt = 1
myInt = 2 // compile-time error!
Strings
var myString = "a"
let myImmutableString = "c"
myString += "b" // ab
myString = myString + myImmutableString // abc
myImmutableString += "d" // compile-time error!
let count = 7
let message = "There are \(count) days in a week"
Logical Operators
var happy = true
var sad = !happy // logical NOT, sad = false
var everyoneHappy = happy && sad // logical AND, everyoneHappy = false
var someoneHappy = happy || sad // logical OR, someoneHappy = true
Printing
let name = "swift"
println("Hello")
println("My name is \(name)")
print("See you ")
print("later")
/* Hello
My name is swift
See you later */
Arrays
var colors = ["red", "blue"]
var moreColors: String[] = ["orange", "purple"] // explicit type
colors.append("green") // [red, blue, green]
colors += "yellow" // [red, blue, green, yellow]
colors += moreColors // [red, blue, green, yellow, orange, purple]
var days = ["mon", "thu"]
var firstDay = days[0] // mon
days.insert("tue", atIndex: 1) // [mon, tue, thu]
days[2] = "wed" // [mon, tue, wed]
days.removeAtIndex(0) // [tue, wed]
Dictionaries
var days = ["mon": "monday", "tue": "tuseday"]
days["tue"] = "tuesday" // change the value for key "tue"
days["wed"] = "wednesday" // add a new key/value pair
var moreDays: Dictionary<string, string=""> = ["thu": "thursday", "fri": "friday"]
moreDays["thu"] = nil // remove thu from the dictionary
moreDays.removeValueForKey("fri") // remove fri from the dictionary
Conditionals
//IF STATEMENT
let happy = true
if happy {
println("We're Happy!")
} else {
println("We're Sad :('")
}
// We're Happy!
let speed = 28
if speed <= 0 {
println("Stationary")
} else if speed <= 30 {
println("Safe speed")
} else {
println("Too fast!")
}
// Safe speed
//SWITCH STATEMENT
let n = 2
switch n {
case 1:
println("It's 1!")
case 2...4:
println("It's between 2 and 4!")
case 5, 6:
println("It's 5 or 6")
default:
println("Its another number!")
}
// It's between 2 and 4!
For Loops
for var index = 1; index < 3; ++index {
// loops with index taking values 1,2
}
for index in 1..3 {
// loops with index taking values 1,2
}
for index in 1...3 {
// loops with index taking values 1,2,3
}
let colors = ["red", "blue", "yellow"]
for color in colors {
println("Color: \(color)")
}
// Color: red
// Color: blue
// Color: yellow
let days = ["mon": "monday", "tue": "tuesday"]
for (shortDay, longDay) in days {
println("\(shortDay) is short for \(longDay)")
}
// mon is short for monday
// tue is short for tuesday
While Loops
var count = 1
while count < 3 {
println("count is \(count)")
++count
}
// count is 1
// count is 2
count = 1
while count < 1 {
println("count is \(count)")
++count
}
//
count = 1
do {
println("count is \(count)")
++count
} while count < 3
// count is 1
// count is 2
count = 1
do {
println("count is \(count)")
++count
} while count < 1
// count is 1
Functions
func iAdd(a: Int, b: Int) -> Int {
return a + b
}
iAdd(2, 3) // returns 5
func eitherSide(n: Int) -> (nMinusOne: Int, nPlusOne: Int) {
return (n-1, n+1)
}
eitherSide(5) // returns the tuple (4,6)
Classes
class Counter {
var count: Int = 0
func inc() {
count++
}
func add(n: Int) {
count += n
}
func printCount() {
println("Count: \(count)")
}
}
var myCount = Counter()
myCount.inc()
myCount.add(2)
myCount.printCount() // Count: 3
[转]Swift Cheat Sheet的更多相关文章
- 转:PostgreSQL Cheat Sheet
PostgreSQL Cheat Sheet CREATE DATABASE CREATE DATABASE dbName; CREATE TABLE (with auto numbering int ...
- Git Cheat Sheet
Merge Undo git merge with conflicts $ git merge --abort Archive $ git archive --format zip --output ...
- CSS3 Animation Cheat Sheet:实用的 CSS3 动画库
CSS3 Animation Cheat Sheet 是一组预设的动画库,为您的 Web 项目添加各种很炫的动画.所有你需要做的是添加样式表到你的网站,为你想要添加动画效果的元素应用预制的 CSS 类 ...
- XSS (Cross Site Scripting) Prevention Cheat Sheet(XSS防护检查单)
本文是 XSS防御检查单的翻译版本 https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sh ...
- IOS Application Security Testing Cheat Sheet
IOS Application Security Testing Cheat Sheet [hide] 1 DRAFT CHEAT SHEET - WORK IN PROGRESS 2 Int ...
- XSS Filter Evasion Cheat Sheet 中文版
前言 译者注: 翻译本文的最初原因是当我自己看到这篇文章后,觉得它是非常有价值.但是这么著名的一个备忘录却一直没有人把它翻译成中文版.很多人仅仅是简单的把文中的 各种代码复制下来,然后看起来很刁的发在 ...
- HTML5 Cheat sheet PNG帮助手册(标签、事件、兼容)
HTML5 Cheat sheet PNG帮助手册(标签.事件.兼容) 1.HTML5标签 2.HTML5事件 3.HTML5兼容 最新HTML5手册资料请参考:http://www.inmotion ...
- The iOS Design Cheat Sheet 界面设计速参
http://ivomynttinen.com/blog/the-ios-7-design-cheat-sheet/ With the release of iOS 7, app designers ...
- Windows平台Atom编辑器的常用快捷键小抄Cheat Sheet
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:Windows平台Atom编辑器的常用快捷键小抄Cheat Sheet.
随机推荐
- C#中 String 格式的日期时间 转为 DateTime
C#中并没有表示时间的变量,只有DateTime,所以要表示时间,可以用TimeSpan表示. 方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-M ...
- 康乐不风流之爱解题的pde灌水王张祖锦
康乐不风流之爱解题的pde灌水王张祖锦 师弟: 邓洪存 (现在烟台大学任教) 好吧, 我一直想写写康乐园里与我相熟的这几个人, 不如趁此机会开始. 第一批人物为张祖锦.苏延辉.张会春.黄显涛.刘兴兴. ...
- 如何解决grails2.3.2中不能运行fork模式
升级到grails 2.3.2之后,运行时报如下的异常: Exception in thread "main" Error | Forked Grails VM exited wi ...
- C#控件前缀命名规范
标准控件 1 btn Button 2 chk CheckBox 3 ckl CheckedListBox 4 cmb ComboBox 5 dtp DateTimePicker 6 lb ...
- 32+激发灵感的HTML5/CSS3网页设计教程
HTML5是寄托在HTML4基础上取得了的广泛成就.这不仅意味着你不必完全放弃现有的一些标记,而是可以借鉴,以加强 它. CSS3也以同样的方式在互联网内容的安排下,提供了它的柔韧性.CSS3是开 ...
- DOS 命令大全
MS DOS 命令大全 一.基础命令 1 dir 无参数:查看当前所在目录的文件和文件夹. /s:查看当前目录已经其所有子目录的文件和文件夹. /a:查看包括隐含文件的所有文件. /ah:只显示出隐含 ...
- 200 OK (from cache) 与 304 Not Modified
解释: 200 OK (from cache) 是浏览器没有跟服务器确认,直接用了浏览器缓存: 304 Not Modified 是浏览器和服务器多确认了一次缓存有效性,再用的缓存. 触发区别: 2 ...
- JS获得月最后一天和js得到一个月最大天数
<html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>标题页</title ...
- 《Java数据结构与算法》笔记-CH4-4循环队列
/** * 循环队列 */ class Queue { private int maxSize; private long[] queue; private int front; private in ...
- 第三百四十九、五十天 how can I 坚持
昨天是忘写博客了,今天下班才突然意思到,搞框架搞了好晚.今天重新下了个好了. 昨天,把存储过程交给同事写了,啥都不会,又一堆问题,折腾了一天. 今天相对轻松些,不过事情还没完..明天又周五了. 还有昨 ...