SWIFT学习笔记(1)----- 简单值声明 Sample Values

来源:互联网 发布:rj45网络接口 编辑:程序博客网 时间:2024/06/06 12:41

基本都是跟着文档走 大家有问题可以反馈一下 大家一起学习

1.简单值声明 Sample Values

  var myConstant =42  ----------可变值

  let myVariable = 42  ------------常量值  “value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once.”编译前就已经被“知道”,必须给一个确定值
let implicitInteger = 70
let implicitDouble = 70.0
let explicitDouble: Double = 70   Double 为声明数据类型  (当不声明数据类型的时候,编译器自动判断数据类型)

声明的变量不能自动转化 数据类型 ,当要转换数据类型时  需要用    数据类型()   来进行声明转换 例如:

let label = "The width is “
let width = 94
let widthLabel = label + String(width)   字符串可以相加?!有木有  

在字符串中用  \( ) 来声明是类型转换  这里代替了OC 和C 的 %d,%s 等
let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit.   注意 :这里可以直接在字符转中运算 

数组,及字典 声明

var shoppingList = ["catfish", "water", "tulips", "blue paint"]
shoppingList[1] = "bottle of water"
 
var occupations = [
    "Malcolm": "Captain",
    "Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations


空数组 和空字典

var emptyArray = String[]()

var emptyDictionary = Dictionary<String, Float>()

0 0
原创粉丝点击