博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]Swift Cheat Sheet
阅读量:6083 次
发布时间:2019-06-20

本文共 3865 字,大约阅读时间需要 12 分钟。

原文: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 = 1var 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 = 1myInt = 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 = truevar sad = !happy // logical NOT, sad = falsevar 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 STATEMENTlet happy = trueif 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 = 1while 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

 

转载于:https://www.cnblogs.com/klaus/p/4249447.html

你可能感兴趣的文章
FZU 2032 高精度小数加法
查看>>
ssh 链接服务器出现 Write failed: Broken pipe
查看>>
Rails console 不能使用,出现cannot load such file -- readline (LoadError) 的解决
查看>>
uva 11468 Substring
查看>>
UVALive-3263 That Nice Euler Circuit (几何欧拉定理)
查看>>
Linux系统Mysql备份的导入导出
查看>>
大道至简第一章感想
查看>>
完美解决PHP中文乱码
查看>>
js获取下拉,单选
查看>>
Spring源码系列 — Envoriment组件
查看>>
zw量化交易·实盘操作·系列培训班
查看>>
repeater 设置分页
查看>>
Linux基础命令一
查看>>
CSRF笔记
查看>>
关于JS的return false
查看>>
HDU - 1789 Doing Homework again 贪心
查看>>
MySQL 忘记密码怎么办?
查看>>
Linux关闭端口
查看>>
ROS在Ubuntu下的安装
查看>>
UML类图关系全面剖析
查看>>