首页 > 代码库 > swift 2.x学习笔记(一)

swift 2.x学习笔记(一)

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #d12f1b } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #4f8187 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #008400 } span.s1 { } span.s2 { color: #bb2ca2 } span.s3 { color: #000000 } span.s4 { font: 14.0px "PingFang SC" } span.s5 { color: #272ad8 } span.s6 { font: 14.0px Menlo } span.s7 { color: #703daa } span.s8 { color: #4f8187 } span.s9 { color: #d12f1b }</style>

//: Playground - noun: a place where people can play

 

import UIKit

 

var str = "Hello, playground"

 

//var 声明变量 很像JavaScript

var myVariable = 42;

myVariable = 50;

 

//let 声明常量 JavaScript 的新标准也是用let C++里的const

let myConstant = 42;

 

let implicitInteger = 70;

let implicitDouble = 70.0;

//显式指定一个常量的类型 (冒号)

let explicitDouble : Double = 70;

 

let explicitFloat : Float = 4;

 

 

let label = "The width is"

let width = 94;

//值永远不会被隐式转换为其他类型。如果你需要把一个值转换成其他类型,请显式转换。 如下,必须把 width 强转为 String才能 label(String 类型)拼接

let widthLabel = label + String(width);

 

let apples = 3;

let oranges = 5;

//有一种更简单的把值转换成字符串的方法:把值写到括号中,并且在括号之前写一个反斜杠

let applesSummary = "I have \(apples) apples.";

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

 

let mingboHeight = 18.00

let mingbo = " \(mingboHeight)" + " mingbo";

//使用方括号 [] 来创建数组和字典,并使用下标或者键(key)来访问元素。最后一个元素后面允许有个逗号。再也不用担心多写一个逗号了

var shoppingList = ["catfish", "water", "tulips", "blue paint"];

shoppingList[1] = "bottle of water";

 

var occupations = [

    "Malcolm":"Captain",

    "Kaylee":"Mechanic",

];

occupations["Jayne"] = "Public Relations";

//要创建一个空数组或者字典,使用初始化语法。

let emptyArray = [String]();

let emptyDictionary = [String :Float]();

//如果类型信息可以被推断出来,你可以用 [] [:] 来创建空数组和空字典——就像你声明变量或者给函数传参 数的时候一样。

shoppingList = [];

occupations = [:];

swift 2.x学习笔记(一)