ios Swift 备忘录

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 = ["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


ios Swift 备忘录,布布扣,bubuko.com

时间: 2024-08-03 03:05:16

ios Swift 备忘录的相关文章

Swift备忘录

Swift 备忘录 2015-4 一.简介 1.Swift 语言由苹果公司在2010年7月开始设计,在 2014 年6月推出,在 2015 年 12 月 3 日开源 2.特点(官方): (1)苹果宣称 Swift 的特点是:快速.现代.安全.互动,而且明显优于 Objective-C 语言 (2)可以使用现有的 Cocoa 和 Cocoa Touch 框架 (3)Swift 取消了 Objective-C 的指针及其他不安全访问的使用 (4)舍弃 Objective-C 早期应用 Smallta

[IOS]swift自定义uicollectionviewcell

刚刚接触swift以及ios,不是很理解有的逻辑,导致某些问题.这里分享一下swift自定义uicollectionviewcell 首先我的viewcontroller不是直接继承uicollectionviewcontroller,而是添加的uicollectionview到我的storyboard, 然后再新建一个swift的文件,让这个swift继承uicollectionviewcell import Foundation class SVGCell :UICollectionView

iOS:Swift界面实例1, 简单界面

Apple推出了基于Objective-C的新语言Swift. 通过实例, 我们可以很好的感受这门新语言 注意事项: 在XCode6_Beta中, 如果有中文, IDE的自动补全功能就会失效, 所以开始调试的时候可以先用英文, 后面再用中文替代. 1. 新建iOS -> Single View Application. 2. 修改AppDelegate.swift文件 1 // 2 // AppDelegate.swift 3 // UIByCode_Swift_1_HelloWorld 4 /

iOS - Swift UISearchController仿微信搜索框

0x01.创建一个UISearchController 如果传入的searchController为nil,则表示搜索的结果在当前控制器中显示,现在我让它在searchVC中显示. // 创建searchResultVC let searchVC = UIViewController() // 设置背景颜色为红色 searchVC.view.backgroundColor = UIColor.red let searchController = UISearchController(search

ios Swift 资源池

Swift入门教程: http://www.cocoachina.com/applenews/devnews/2014/0604/8661.html Swift视频教程: http://www.cocoachina.com/bbs/read.php?tid=204280 Swift官方文档(PDF版): http://www.cocoachina.com/bbs/read.php?tid=204446 Swift官方文档(网页版): https://developer.apple.com/lib

ios Swift 之github

1. 软件类 a) 作者集合 http://nondot.org/sabre/ b) swift for facebook SWIFT是一个易于使用的,基于注解的Java来创建勤俭节约序列化类型和服务库. https://github.com/facebook/swift 2. 游戏类 a)FlappySwift https://github.com/fullstackio/FlappySwift b) 2048 https://github.com/austinzheng/swift-2048

ios swift reduce Method

Swift’s API includes many functions and instance methods that reflect its functional programming heritage. A prime example is called reduce.  You can reduce a collection of values down to just one value, such as calculating the sum of every person’s

ios Swift 算法

// Playground - noun: a place where people can play import Cocoa var nums = Int[]() for _ in 1...50 { nums.append(random()) } nums ////冒泡排序 /* var count = 0; for(var i = 0 ; i < nums.count-1; i++){ for(var j = 0; j < nums.count-i-1;j++){ count++; if

ios Swift 国外资源

Swift国外资源汇总(No.1) 此类分享贴暂定每2天更新一次,主要目的是让大家能跟国外开发者们同步,共享知识和共同提高. 对于一些非常有价值的文章,大家有兴趣可以自行翻译(回贴跟我说一声,避免重复劳动,之后发布到论坛或自己blog都可以),我也会将相关链接同步到本贴. 编程思想 Why Objective-C is doomed主要观点:1. Swift跟ObjC互用做的非常好 2. 预计未来会出现Swift-first趋势,官方库和第三方库都会优先考虑Swift实现 3. iOS&OS X