http://kpbp.github.io/swiftcheatsheet/#constants
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 = ["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
Closures
分享到:
相关推荐
本人也是初学者,第一次见到单例就很懵,经过大量的搜索自己总结了单例的简单理解
或许有这样的需求,要求页面在无网...我总结了一个库,可以异步按照目录删除。 。简单方便,快捷神速,是居家iOS码农必备之利器,具体功能模块如下。 具体代码请移步:https://github.com/dudongge/DDGDataCache_Swift
在 Mac 上学习 Swift 非常简单,你只需在 App Store 中安装 Xcode 即可。接下来,你有两种方法来学习 Swift 基础语法。 方法一:通过命令行 你只需打开 Terminal 运行 swift 命令即可: $ swift Welcome to Apple...
在 Mac 上学习 Swift 非常简单,你只需在 App Store 中安装 Xcode 即可。接下来,你有两种方法来学习 Swift 基础语法。 方法一:通过命令行 你只需打开 Terminal 运行 swift 命令即可: $ swift Welcome to Apple...
个人学习中简单写的App,实现普通计算器所有功能,支持连续计算,支持乘方等。没来得及做适配,目前仅适合竖屏Apple X以后版本
X UI是使用SwiftUI创建模块化,可重用,可测试的应用程序体系结构的... 有用的扩展使SwiftUI和Combine的使用更加简单! :male_sign::male_sign: 入门 店铺 X UI的组成部分之一是属性包装器。 它使使用协议定义Swift
中有解决方案和一些总结博客文章。 解决方案 , 容易 292: , , 容易 20: , 位操作 , 中 136: , 数学 , 简单的 204: , , 容易 231: , , 易 326: , , 参考 , 易 258: , , 参考 , 简单的 263: , , 简单的 202: , , 容易 ...
简单值 收藏类型 类型 控制流 控制流 表达式 函数和闭包 职能 声明 对象和类 关闭 声明 枚举和结构 枚举 属性 协议和扩展 类和结构 图案 错误处理 特性 通用参数和参数 泛型 方法 语法总结 下标 遗产 初始化 取消...
总结该库提供了一种简单的方法,可以在Linux上使用Swift来使用数字GPIO,SPI / I2C接口,1-Wire总线,PWM信号和Raspberry Pi等板提供的串行端口与外部传感器和设备进行交互。 与Android Things或Python中的类似库...
23种设计模式swift实现 一、设计模式七大原则(有些地方使用六大原则,没有第六调,此处主要是总结,用于学习) 1、单一职责原则 一个类只负责一个功能领域中的相应职责 2、里氏替换原则 任何基类可以出现的地方,...
作者mokey1422,源码GBAliScan,仿写的类似支付宝...本demo是我最近在整理的oc教程篇中的其中一篇,以后还会陆续出一些oc上的教程,有的侧重功能有的侧重知识点的汇总,等完成oc的扫尾工作就开始学习和总结swift了
总结 编码风格 - [Swift]( ) Swift编码风格指南。 [Objective-C]( ) Objective-C编码风格指南。 构架 - [ReactiveCocoa]( ) 一个功能性的React式编程框架,提供用于组合和转换值流的API。 ...
StackViewController是一个Swift框架,可简化使用UIStackView构建表单和其他静态内容的UIStackView 。 例如,下面的表格是使用StackViewController实现的: 设计原理 该项目的目的有两个:鼓励采用更适合于构建内容...
进度条在iOS开发中很常见的,我在项目开发中也写过好多进度条,有好多种类的,条形,圆形等,今天给大家总结一种条形的开发进度条。 简单思路: 1.自定义进度条先继承UIView 建立一个CustomBarProgressView 2....
无线移动通信正Swift进入5G技术时代。 与其他有线网络技术相比,对无线通信不断增长的需求基本上是由于非常简单的安装和维护。 因此,对宽带和高速移动通信的快速增长的需求需要非常高速的网络,其能够满足增长的...
自己总结的iOS、mac开源项目及库,持续更新。star 5.6k :red_heart: :red_heart: :red_heart: :red_heart: :red_heart: 一个很棒的 iOS UI/UX 库的精选列表明星 8k 适用于 iOS 的一组很棒的平面 UI 组件。 7k 精心...
而收藏,当然是为了在可能需要的时候能够Swift果断地找到一个合适我们项目的轮子。 分类 二级分类 框架名称 简介 Star 数 最近更新 UI Loading 智能下拉刷新框架 21k 25天 UI Loading 比较早的一款下拉刷新框架 8.8k...
是谷歌开源的 serverless 架构方案,旨在提供一套简单易用的 serverless 方案,把 serverless 标准化。目前参与的公司主要是 Google、Pivotal、IBM、Red Hat,2018年7月24日才刚刚对外发布,当前还处于快速发展的...
数据访问瓶颈 大数据处理平台的特点 通过分布式计算框架来实现 提供高效的计算模型和简单的编程接口 可扩展性:通过增加资源以满足不断增加的性能和功能需求 容错能力:考虑系统的不可靠性,支持出现错误后自动恢复 ...
之前的算法和数据结构基本都是用Swift写的,现在尝试用Python实现一些简单的算法和数据结构。 update 20160704 准备加入的习题python实现,以及机器学习过程中的一些算法 update 20160717 加入leetcode部分 ## update...