swift 2.x學習筆記(一)

來源:互聯網
上載者:User

標籤:冒號   bsp   方法   lips   list   cap   paint   class   constant   

//: 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學習筆記(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.