Swift Tour 随笔总结 (1)

let Constant

var Variable


let implicitInteger = 70
let implicitDouble = 70.0
let explicitDouble: Double = 70

The so-called type implications

To include value in strings:

let fruitSummary = "I have \(apples + oranges) pieces of fruit."
 

Arrays and Dicts:

var shoppingList = ["catfish", "water", "tulips"]
shoppingList[1] = "bottle of water"
var occupations = [
    "Malcolm": "Captain",
    "Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
//empty
let emptyArray = String[]()
let emptyDictionary = Dictionary()
 

Control Flow and Optional Binding

For each


let individualScores = [75, 43 103, 87, 12]
var teamScore = 0
for score in individualScores {
    if score > 50 {
        teamScore += 3
    } else {
        teamScore += 1
    }
}
teamScore

Optional Binding


var optionalString: String? = "Hello"
optionalString == nil

var optionalName:String? = "John Appleseed"
var greeting = "Hello!"

//optional binding
if let name = optionalName {
    greeting = "Hello, \(name)"
}

If the optional value is nil, the conditional is false and the code in braces is skipped.

Otherwise, the optional value is unwrapped and assigned to the constant after let, which makes the unwrapped value available inside the block of code.

时间: 2024-12-09 02:28:30

Swift Tour 随笔总结 (1)的相关文章

Swift Tour 随笔总结 (4)

Switch的一个例子: let vegetable = "red pepper" switch vegetable { case "celery": let vegetableComment = "Add some raisins and make ants on a log." case "cucumber", "watercress": let vegetableComment = "Tha

Swift Tour 随笔总结 (3)

关于Optional的Control Flow if let constantName = someOptional {     statements } 如果该Optional为nil,则不进入if,否则执行且constantName为该Optional的值 例子: if let actualNumber = possibleNumber.toInt() {     println("\(possibleNumber) has an integer value of \(actualNumbe

[IOS]《A Swift Tour》翻译(一)

以下翻译内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3768936.html 碎碎念... Swift是苹果在WWDC刚发布的新语言,本人也没学过,现在看苹果官方文档一边翻译一边学习,再加上英语水平和对编程理解很有限,有错误的地方请大家指出,翻译只供参考,建议阅读苹果Swift官方的文档 Swift 之旅 按照传统,在开始学习一门新的语言时写的第一个程序应该是在屏幕上打印"Hello, World",这个可以

Swift学习——A Swift Tour 枚举和结构体

Enumerations and Structures Enumerations   枚举的使用 使用 enum 定义一个枚举,枚举里面可以关联方法,比如下文中的描述方法 enum Rank: Int { case Ace = 1 case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten case Jack, Queen, King func simpleDescription() -> String { switch self { ca

A Swift Tour

首先说下自己对Swift语言的一点点看法,对于一个写过javascript和常年写java代码的人来说,学习Swift是一件很简单的事情.就像某某人说过,每个人都有弱点和优点,我到目前为止,只是初步的认识这门语言,两个字扩散.或许,弱语言就是这样吧. 一.基础知识 1.基本类型 整型:Int 浮点型:Float,Double 字符型:String 布尔型:Bool 集合类型:Array,Dictionary ps:基本类型首字母要大写,基本类型不能直接定义(例如不能像java那样int a =

Swift学习——A Swift Tour 函数

Functions and Closures  函数和封闭性(闭包) Functions  函数的使用 Swift中的函数定义和OC中有明显的差别了,使用func定义函数,在括号里定义參数和类型,用 -> 定义返回值类型 func greet(name: String, day: String) -> String { return "Hello \(name), today is \(day)." } greet("Bob", "Tuesda

A Swift Tour(3) - Functions and Closures

Functions and Closures 使用func来声明函数,通过括号参数列表的方式来调用函数,用 --> 来分割函数的返回类型,参数名和类型,例如: func greet(name: String, day: String) -> String { return "Hello \(name), today is \(day)." } greet("Bob", day: "Tuesday") //这是swift文档中的调用方法

A Swift Tour(4) - Objects and Classes

Objects and Classes(对象和类) 用 class 关键字后面跟一个类名来创建一个class,在一个类中声明 常亮或变量,他存在于当前类的上下文,函数的方法是同样的 var numberOfSides = 0 let numberOfSidesLet = 1 func simpleDescription() -> String { return "A shape with \(numberOfSides) \(numberOfSidesLet) sides." }

寒城攻略:Listo 教你 25 天学会 Swift 语言 - 02 Swift Tour

import Foundation //*********************************************************************************************** //1.Hello world //_______________________________________________________________________________________________ //输出 "Hello, world&q