swift学习之定义常量、变量、字典、数组
2016-03-23 19:09
537 查看
//1.定义一个常量和变量,定义的量如果没有说明类型,编译器会自动判断
//定义一个常量 let声明,只准赋值一次多次使用,二次赋值会报错
let myConstant = 42
//打印(这里采用了(常量或变量)的形式直接将其他类型转化为字符串String类型)
print(“常量的值是(myConstant)”)
//2.定义的常量和变量不会隐式转换为其他类型,如果想转就得用显式转换
let label = “The width is”
let width : NSInteger = 94
let widthLabel = label + String(width)
print(widthLabel)
//3.用[]来创建数组和字典,并使用下标或(key)来访问元素
//创建一个数组 <>内表示数组元素的类型,有时也有var strarr:String[] = [“理想”,”swift”]这种写法
var shoppingList:Array = [“water”,”apples”,”milk”,”cake”]
//增、追加
shoppingList.insert(“chick”, atIndex: 0)
shoppingList.append(“appendElm”)
shoppingList += [“追加的”]
//改
shoppingList[1] = “an apple”
//查
let str0 :String = shoppingList[0]
//删 根据索引删、全删、删最后一个、删第一个
shoppingList.removeAtIndex(2)
//shoppingList.removeAll()
//shoppingList.removeLast()
//shoppingList.removeFirst()
print(shoppingList,str0)
//拓展
print(“数组的元素个数为:(shoppingList.count)”)
//遍历 此处的EnumerateSequence会在swift3.0移除,届时由enumerate()替代
for elm in EnumerateSequence(shoppingList) {
print(“元素下标:(elm.0) 元素值:(elm.1) (elm)”)
}
//构造语法来创建数组
//创建一个Int数据类型构成的空数组
var nums = Array()
//创建一个自定义数据类型构成的空数组
class MyClass{
//定义一个常量 let声明,只准赋值一次多次使用,二次赋值会报错
let myConstant = 42
//打印(这里采用了(常量或变量)的形式直接将其他类型转化为字符串String类型)
print(“常量的值是(myConstant)”)
//定义一个变量 var声明,变量的值可以多次赋值,最终赋值即为变量的值 var myVariable = 2 myVariable = 32 print(myVariable) //如果声明的量是指定类型的,可以在名后冒号加类型,如下Float类型 let aFloatContant:Float = 40.2 print(aFloatContant)
//2.定义的常量和变量不会隐式转换为其他类型,如果想转就得用显式转换
let label = “The width is”
let width : NSInteger = 94
let widthLabel = label + String(width)
print(widthLabel)
//3.用[]来创建数组和字典,并使用下标或(key)来访问元素
//创建一个数组 <>内表示数组元素的类型,有时也有var strarr:String[] = [“理想”,”swift”]这种写法
var shoppingList:Array = [“water”,”apples”,”milk”,”cake”]
//增、追加
shoppingList.insert(“chick”, atIndex: 0)
shoppingList.append(“appendElm”)
shoppingList += [“追加的”]
//改
shoppingList[1] = “an apple”
//查
let str0 :String = shoppingList[0]
//删 根据索引删、全删、删最后一个、删第一个
shoppingList.removeAtIndex(2)
//shoppingList.removeAll()
//shoppingList.removeLast()
//shoppingList.removeFirst()
print(shoppingList,str0)
//拓展
print(“数组的元素个数为:(shoppingList.count)”)
//遍历 此处的EnumerateSequence会在swift3.0移除,届时由enumerate()替代
for elm in EnumerateSequence(shoppingList) {
print(“元素下标:(elm.0) 元素值:(elm.1) (elm)”)
}
//构造语法来创建数组
//创建一个Int数据类型构成的空数组
var nums = Array()
//创建一个自定义数据类型构成的空数组
class MyClass{
} var myClass = MyClass() //创建特定个数并且所有数组被默认特定值的数组,一般用于状态取反,如记录按钮点击与否 var tempArray = Array<Int>(count: 5, repeatedValue:3)//5个3的数组 //创建一个字典<,>尖括号内表示key的类型和value的类型,也可不写,编译器自动识别 var priceOfShopping:Dictionary<String,Float> = ["waterPrice":2.0,"applesPrcie":7.76,"cakePrcie":0.250] //增 priceOfShopping["meat"] = 14.99 //删 //priceOfShopping.removeAll() priceOfShopping.removeValueForKey("cakePrice") //改 priceOfShopping.updateValue(99.99, forKey: "waterPrice") //查 let aPrice = priceOfShopping["meat"] print(priceOfShopping,aPrice) //遍历循环(顺序和无序) for (key,value) in priceOfShopping { print("有序输出 键:\(key) 值:\(value)") } for key in priceOfShopping.keys { print(key) } for tuples in priceOfShopping { print("无序输出 键:\(tuples.0) 值:\(tuples.1) \n\(tuples)") } //此处输出的是LazyMapCollection,依旧是字典类型 let arrayOfKey = priceOfShopping.keys print(arrayOfKey) //构造方法创建字典 var tempDic = Dictionary<String, Int>()
相关文章推荐
- 初见swift--基础运算符
- DFBle.swift
- Swift 3.0 最基本的多线程
- Swift 引入第三方字体库
- 初见swift--基本类型
- swift系统学习第三章
- swift TabBarController UINavigationController 组合使用 带有ScrollView和菜单内容以及tableView
- swift调用高端地图
- Swift 值类型和引用类型
- Swift元组
- swift set
- 【Swift】iOS UICollectionView 计算 Cell 大小的陷阱
- 28.swift高级
- 27.swift基础
- swift的一些常见用法
- sqlite系列(2)使用FMDB保存数据swift
- 实现类似于格瓦拉启动页中的放大转场动画(OC&Swift)
- swift学习(基本运算符,字符和字符串)
- OC与Swift混编
- swift闭包--简明代码理解