Variables

var myInt =
var myExplicitInt: Int = // explicit type
var x = , y = , z = // declare multiple integers
myExplicitInt = // set to another integer value

Constants

let myInt =
myInt = // compile-time error!

Strings

var myString = "a"
let myImmutableString = "c"
myString += "b" // ab
myString = myString + myImmutableString // abc
myImmutableString += "d" // compile-time error! let count =
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[] // mon
days.insert("tue", atIndex: ) // [mon, tue, thu]
days[] = "wed" // [mon, tue, wed]
days.removeAtIndex() // [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 = ["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 =
if speed <= {
println("Stationary")
} else if speed <= {
println("Safe speed")
} else {
println("Too fast!")
}
// Safe speed //SWITCH STATEMENT
let n =
switch n {
case :
println("It's 1!")
case ...:
println("It's between 2 and 4!")
case , :
println("It's 5 or 6")
default:
println("Its another number!")
}
// It's between 2 and 4!

For Loops

for var index = ; index < ; ++index {
// loops with index taking values 1,2
}
for index in .. {
// loops with index taking values 1,2
}
for index in ... {
// 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 =
while count < {
println("count is \(count)")
++count
}
// count is 1
// count is 2 count =
while count < {
println("count is \(count)")
++count
}
// count =
do {
println("count is \(count)")
++count
} while count <
// count is 1
// count is 2 count =
do {
println("count is \(count)")
++count
} while count <
// count is 1

Functions

func iAdd(a: Int, b: Int) -> Int {
return a + b
}
iAdd(, ) // returns 5 func eitherSide(n: Int) -> (nMinusOne: Int, nPlusOne: Int) {
return (n-, n+)
}
eitherSide() // returns the tuple (4,6)

Classes

class Counter {
var count: Int =
func inc() {
count++
}
func add(n: Int) {
count += n
}
func printCount() {
println("Count: \(count)")
}
} var myCount = Counter()
myCount.inc()
myCount.add()
myCount.printCount() // Count: 3

												

ios Swift 备忘录的更多相关文章

  1. Swift备忘录

    Swift 备忘录 2015-4 一.简介 1.Swift 语言由苹果公司在2010年7月开始设计,在 2014 年6月推出,在 2015 年 12 月 3 日开源 2.特点(官方): (1)苹果宣称 ...

  2. iOS swift的xcworkspace多项目管理(架构思想)

    iOS  swift的xcworkspace多项目管理(架构思想) 技术说明: 今天在这里分享 swift下的 xcworkspace多项目管理(架构思想),能为我们在开发中带来哪些便捷?能为我们对整 ...

  3. iOS Swift 模块练习/swift基础学习

    SWIFT项目练习     SWIFT项目练习2 iOS Swift基础知识代码 推荐:Swift学习使用知识代码软件 0.swift中的宏定义(使用方法代替宏) 一.视图  +控件 1.UIImag ...

  4. ios swift 实现饼状图进度条,swift环形进度条

    ios swift 实现饼状图进度条 // // ProgressControl.swift // L02MyProgressControl // // Created by plter on 7/2 ...

  5. Building gRPC Client iOS Swift Note Taking App

    gRPC is an universal remote procedure call framework developed by Google that has been gaining inter ...

  6. iOS Swift WisdomScanKit图片浏览器功能SDK

    iOS Swift WisdomScanKit图片浏览器功能SDK使用 一:简介      WisdomScanKit 由 Swift4.2版编写,完全兼容OC项目调用. WisdomScanKit的 ...

  7. iOS Swift WisdomScanKit二维码扫码SDK,自定义全屏拍照SDK,系统相册图片浏览,编辑SDK

    iOS Swift WisdomScanKit 是一款强大的集二维码扫码,自定义全屏拍照,系统相册图片编辑多选和系统相册图片浏览功能于一身的 Framework SDK [1]前言:    今天给大家 ...

  8. iOS设计模式 - 备忘录

    iOS设计模式 - 备忘录 原理图 说明 1. 在不破坏封装的情况下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可以将该对象恢复到原先保存的状态 2. 本人已经将创建状态与恢复状态 ...

  9. iOS Swift WisdomHUD 提示界面框架

    iOS Swift WisdomHUD 提示界面框架  Framework Use profile(应用简介) 一:WisdomHUD简介 今天给大家介绍一款iOS的界面显示器:WisdomHUD,W ...

随机推荐

  1. 转载 GUID介绍

    转载 http://www.cnblogs.com/illele/archive/2008/02/25/1080554.html GUID(Global unique identifier)全局唯一标 ...

  2. [置顶] HTML语义和前端架构

    关于语义学 语义学是研究符号和意义之间的关系以及它们表示的内容.在语言学中,则主要是研究符号(例如单词,短语或者语音)在语言中所表达的意义.而在前端开发时,语义学则更多的关注HTML元素,属性以及它的 ...

  3. C#将对象转换成JSON字符串,Newtonsoft.Json (JSON.NET)

    官方API说明文档 http://www.newtonsoft.com/json/help/html/N_Newtonsoft_Json.htm http://www.newtonsoft.com/ ...

  4. 【Away3D代码解读】(四):主要模块简介

    数据模块: Away3D中最核心的数据类是Mesh类,我们先看看Mesh类的继承关系: NamedAssetBase:为对象提供id和name属性,是Away3D大部分类的基类: Object3D:3 ...

  5. Tokumx 安装指南(做法如同MongoDB)

    安装说明系统环境:Centos-6.3安装软件:mongodb-linux-x86_64-2.2.2.tgz下载地址:http://www.mongodb.org/downloads安装机器:192. ...

  6. lua string 库

    --lua中字符串索引从前往后是1,2,……,从后往前是-1,-2……. --string库中所有的function都不会直接操作字符串,只返回一个结果. ---------------------- ...

  7. HDU4279(2012年天津网络赛---数论分析题)

    题目:Number 题意: 给出一个f(x),表示不大于x的正整数里,不整除x且跟x有大于1的公约数的数的个数.定义F(x),为不大于x的正整数里,满足f(x)的值为奇数的数的个数.题目就是求这个F( ...

  8. 清空session的方法

    清空session的方法,常用来注销的时候清空所有的session. 方法一: Enumeration e=session.getAttributeNames(); while(e.hasMoreEl ...

  9. 对PostgreSQL cmin和cmax的理解

    看例子: 开两个终端来对比: 在终端A: [pgsql@localhost bin]$ ./psql psql () Type "help" for help. pgsql=# b ...

  10. C++Vector使用方法

    C++内置的数组支持容器的机制,可是它不支持容器抽象的语义.要解决此问题我们自己实现这种类.在标准C++中,用容器向量(vector)实现.容器向量也是一个类模板.标准库vector类型使用须要的头文 ...