Tradition suggests that the first program in a new language should print the words "Hello ,world!" on the screen. In Swift , this can be done in a single line :  print("Hello world")

If you gave written code in C otr Objective - C , this syntax looks famiiar to you --- in Swift . this line of code is a complete program. Youdon't need to import a separate library for functionality like input /output or string handling . Code written at global scope is used sa the entry point for the program , so you don't need a main () function . You also don't need to write semicolons at the end of every statement .

This tour gives you enough information to start writing code in Swift by showing you how to accomplish a variety of programming tasks . Don't worry if you don't understand something --everything introduced in this tour is explained in detail in the rest of this book.

Simple Values

Use ler to make a constant and var to make a variable . The value of a constant doesn't need to be known at compile time , but you must assign it a value exactly once . This means you can use constants to name a value that you determine once but use in many places .

var myVariable = 42

         myVariable  = 50

         let myConstant = 42

A constant or variable must have the same type as the value you want to assign to it. However, you don't always have to write the type explictly.Providing a vakue when you creare a constant or variable lets the conpiler infer its type . In the exmple  above , the compiler infers  that myVariable is  an integer because its initial value is an integer.

If the  initial value doesn't provide enough information (or if there is no initial value ), specify the type by writing it after the variable , separated by a colon.

let implicitInterger = 70

               let impicitDouble = 80.0

               let explisitDouble : Double = 70

Values are never implicitly converted to  another type . If you need to convert a value to a different type, explicitly make an instance of the desired type .

      let label = " The width is "

      let width = 94

      let widthLable = lable + String (width)

There's an even simpler way to include balues in string : write the value in parentheses, and write a backslash (\) before th parentheses . For example:       let apples = 4

      let oranges = 5

      let appleSummary = " I have \ (apples) apples."

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

Create arrays  and dictionaries using brackets ([]), and access their elements by writing the index or key in brackets . A comma is allowed is allowed after the last element.

var shoppingList  = ["catfish","water"]

   shoppingList [1] = "bottle of water"

   var occupations = ["Malcolm":"Captain"]

    occupations ["Jayne"] = "Public Relations"

To create an empty array or dictionary, use the initializer syntax.

    let emptyArray = [String] ()

    let emptyDictionary = [String : float]()

If type information can be inferred , you can write an empty array as [] and an empty dictionary as [:] -- for example , when you set a new value for a variable or pass ab argument to a function.

    shoppingList = []

    occupation = [:]

Control Flow

Use if and switch to make conditionals , and use for - in , while ,and repeat- while to make loops . Paretheses around the condition or loop variable are optional . Braces around the body are required.

    let indviidualScores = [75, 43 , 103, 87 12]

    var teamScore = 0

    for score in individualScores {

if score > 50{

  teamScore += 3

}

  else {

  teamScore += 1

}

}

print(teamScore)

In a if statement, the conditional must be a Boolean expression --- this means that code such as if score {...} is an error, not an implicit comparison to zero.

You can use if and let together ro work with values that might be missing . These values are represented as optionals. An optional value either contains a value or contains nil to indicte that a value a is missing . Write a question mark (?) after the type of a value to mark the value as optional .

   var optionalString :String? = "Hello"

  print(optionalString == nil )

  var optionalName :string? = "john appleseed"

  var greeting = "Hello!"

  if let name = optionalName {

  greetiing = "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 constnat after let , which makes the unwrapped value available inside the block of code .

Switches support any kind of data and a wide variety of comparison operations -- they aren't limited to integers and tests for equality.

A Swifr Tour的更多相关文章

  1. POJ 1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9276   Accepted: 3924 ...

  2. Euler Tour Tree与dynamic connectivity

    Euler Tour Tree最大的优点就是可以方便的维护子树信息,这点LCT是做不到的.为什么要维护子树信息呢..?我们可以用来做fully dynamic connectivity(online) ...

  3. POJ2677 Tour[DP 状态规定]

    Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4307   Accepted: 1894 Description ...

  4. soj 1015 Jill's Tour Paths 解题报告

    题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...

  5. poj1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8859   Accepted: 3728 ...

  6. A quick tour of JSON libraries in Scala

    A quick tour of JSON libraries in Scala Update (18.11.2015): added spray-json-shapeless libraryUpdat ...

  7. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  8. POJ 1637 Sightseeing tour (混合图欧拉回路)

    Sightseeing tour   Description The city executive board in Lund wants to construct a sightseeing tou ...

  9. POJ2135 Farm Tour

      Farm Tour Time Limit: 2MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description ...

随机推荐

  1. 02 Linux常见命令

    GUI图形界面 图形界面对于我们的Linux系统来说就是一个单独的软件程序,可以安装也可以不用安装: 我们常见的Linux下的常用图形软件为Gnome.KDE.XFce: GLI命令界面 Linux常 ...

  2. Memory Translation and Segmentation.内存地址转换与分段

    原文标题:Memory Translation and Segmentation 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的精 ...

  3. elasticsearch License 到期后更新步骤

    ELK下载安装后有一个月试用期,到期后需要更新License,且每个License有效期为 1 年,License过期前10天里相关log会一直出现,提醒用户更新,支持实时更新无需重启服务. 步骤: ...

  4. Ubuntu下RabbitMQ安装

    由于RabbitMQ需要erlang语言的支持,在安装RabbitMQ之前需要安装erlang,执行命令: sudo apt-get install erlang-nox 安装RabbitMQ命令: ...

  5. zepto引用touch模块后,click失效

    近日,有个拼图小活动,引用了zepto,以及zepto的touch模块. 在拼图结束之后,进行抽奖的活动,该抽奖结果是以弹框展示. 这里的关闭按钮需要添加点击事件: $(document.body). ...

  6. oracle 多行合并为一行

    sys_connect_by_path select i,ltrim(max(sys_connect_by_path(a,',')),',') afrom(select i,a,d,min(d) ov ...

  7. 使用spacedesk实现两台笔记本的双屏显示

    转自:https://blog.csdn.net/qq_28106269/article/details/79297132 共屏软件:https://spacedesk.net/

  8. 数据持久化—真机上的Plist写入

    其实写入不到真机里面主要是你写入时,当前那文件夹里你要写入的plist根本不存在 所以你怎么写都写不进去,所以你必须先创建你要写入的plist; 你用这样初始化程序就能自己创建: NSMutableA ...

  9. python学习笔记_week7

    一.面向对象编程进阶 静态方法只是名义上归类管理,实际上在静态方法里访问不了类或实例中的任何属性 class Dog(object): def __init__(self,name): self.na ...

  10. 类库、委托、is/as

    一.类库(Class Library) 定义:类库是一个综合性的面向对象的可重用类型集合,这些类型包括:接口.抽象类和具体类.(内容不可见) 类库建立:文件--新建--项目--类库--生成解决方案 类 ...