在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、常量与变量 var testVariable = 42 二、命名规则 let doubleNum:Double = 2.1 三、类型别名 typealias typealias NSInteger = Int 四、Playground 五、Array // 不支持隐式类型转换
// 相同类型相加 var widthLabel = label + String(width) widthLabel += "!" println(widthLabel) let apple = 3 let oranges = 5 let applesSummary = "I have \(apple + oranges) apples" println(applesSummary)
var shoppingList = ["芒果","橘子","葡萄"]
println(shoppingList.capacity) // 增加元素 shoppingList.append("香蕉") shoppingList.insert("苹果", atIndex: 0) //shoppingList += "柚子" shoppingList += ["西瓜","木瓜","杨桃"] println(shoppingList) println(shoppingList.count) println(shoppingList.capacity) shoppingList[0] = "哈密瓜" println(shoppingList) shoppingList[4...7] = ["Bananas","Apples"] // 把4.5.6.7下标元素换成 // 除了能改变元素以外,还会改变元素个数 println(shoppingList) // 删除 shoppingList.removeLast() println(shoppingList) shoppingList.removeAtIndex(4) // 数据的遍历 for item in shoppingList { println(item) } // 遍历2 for (index,value)in enumerate(shoppingList) { println("Item \(index+1):\(value)") } var threeDoubles = [Double](count: 3, repeatedValue: 0.0) var anotherDoubles = Array(count: 3, repeatedValue: 2.5) println(threeDoubles) println(anotherDoubles) var sixDoubles = threeDoubles + anotherDoubles println(sixDoubles)
六、Dictionary let emptyDictionary = [:] let emptyDic = Dictionary<String,Float>() var airports = ["TYO":"Tokyo","DUB":"Dublin"] println(airports) // 添加和修改字典 airports["TYO"] = "Tokyo International" println(airports)
// 假如没有找到这个key 就要添加一对 airports["LHR"] = "London" println(airports) // delete airports["LHR"] = nil println(airports) // airports.removeValueForKey("TYO") println("airports count is " + String(airports.count)) println("airports count is \(airports.count)") // 遍历 for (key,value) in airports { println("\(key):\(value)") } // 遍历所有的key for key in airports.keys { println(key) } let allKeys = airports.keys.array println(allKeys) 七、Tuple // Tuple元组 :任意数据类型的容器,常作为函数返回值 let (x,y) = (1,2)
let http404Error = (404,"Not Found") let (statusCode,statusMessage) = http404Error // 下划线— 忽略不需要的元素 let (justTheStatusCode,_) = http404Error
// 直接访问 println("just is \(justTheStatusCode)") // 类似于字典 用key println("status is \(x)") // 用序号index println("status code is \(http404Error.0)") let http200Status = (statusCode:200,desc:"OK") println("http200: \(http200Status.statusCode)") 八、optional // Optional 要么不存在 要么等于X var serverResponseCode :Int? = 404 let possibleNumber = "123" let converNumber: Int? = possibleNumber.toInt() // 假如是123可以转换成int,则存在,假如是Hello 就没办法转换成int 也就不存在了 println(converNumber?.description) // nil 不能用与 非可选类型 // 假设代码中有变量或常量 可能不存在 ,就要声明成恰当的可选类型。如果定义的可选的类型对象不提供默认值,则该对象自动设为nil var strValue : String? var strTest : String? = "hello" let hashValue = strValue?.hashValue println(hashValue) println(strTest?.hashValue) /* 总结 ?使用场景 1、声明optional变量 2、optional值操作中,判断是否操作。 */ 声明UI 代码的时候会用 var window: UIWindow?
九、解包 ! self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window!.backgroundColor = UIColor.greenColor() var rootViewController = RootViewController() var rootNavigationController = UINavigationController(rootViewController: rootViewController) self.window!.rootViewController = rootNavigationController
let possibleString: String? = "an optional string" println(possibleString!)// 解包,确定一定存在值。 let possibleString:String? = "An optional string" let stringVale = possibleString!.hashValue // 这种方法叫选择绑定 if let sValue = possibleString { let stringValue = sValue.hashValue } 十、隐式解包的可选类型 代码很多的话,可选类型 每次都要写感叹号 ! 不方便 定义的时候 直接把问号改成感叹号 隐式解包的可选类型:自动解包的可选类型,用在:定义常量变量,定义完成后一定会存在的情况 主要是用在类的初始化过程中。 let assumedString: String! = "an test string" println(assumedString) println(assumedString.hashValue) 总结!使用场景 1、强制对 可选类型进行解包 2、声明 隐士解包的可选类型 类的属性 |
请发表评论