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. 关于python中生成器之Send方法

    #send主要是用于外部与生成器对象的交互def func1(): # 生成器函数 print("ok1") x = 10 # 函数内局部变量x赋值为10 print(x) x = ...

  2. mysql查询当前时间,一天内,一周,一个月内的sql语句

    查询一天:select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ...

  3. WPF DataGrid 数据绑定、样式、分页、增删改查,连接Access数据库

    先上效果图: XAML: <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q ...

  4. 可恶的 0xc0000005异常

    昨天快下班的时候自己写的代码突然跑出了这个异常,,搞的我特别的无语,因为昨天女朋友要过来了,给我出现了这么一个bug,心里很不爽,果断低下班了,另外一个原因就是公司其他人都跑了,每次基本都是我一个人最 ...

  5. 在eclipse中安装go编辑器阅读fabric代码

    参考资料 由于fabric采用go语言编写,故需要安装go环境. 安装要求: Java VM version 8 or later. Eclipse 4.6 (Neon) or later. 1. J ...

  6. kafka源代码环境配置

    kafka版本10.0.0.没有采用最新版本是因为项目中目前使用了这个版本. 1.安装gradle 首先进入https://gradle.org/install 查看Install manually- ...

  7. thinkphp3.2.2有预览的多图上传

    thinkphp3.2.2有预览的多图上传 整体思路 1 封装文件上传和图片上传的类文件 2 视图中添加相关JS和表单提交 3 控制器中添加上传文件的相关代码 一 2个class 文件 请上传到/Th ...

  8. 《算法》BEYOND 部分程序 part 2

    ▶ 书中第六章部分程序,加上自己补充的代码,包括快速傅里叶变换,多项式表示 ● 快速傅里叶变换,需要递归 package package01; import edu.princeton.cs.algs ...

  9. windows定时关机命令

    at 11:00 shutdown.exe -s -t 120 https://jingyan.baidu.com/article/574c52191ea9996c8c9dc17a.html?st=2 ...

  10. EventBus用法

    什么是EventBus EventBus是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间.组件与后台线程间的通信.比如请求网络,等网络返回时通过Hand ...