一些Swift小知识点的整理

一些Swift小知识点的整理,第1张

概述之前最开始了解Swift的时候的一些基本的东西,简单的整理一下。 import UIKitvar names = []class ViewController: UIViewController,UIAlertViewDelegate { override func viewDidLoad() { super.viewDidLoad() var st

之前最开始了解Swift的时候的一些基本的东西,简单的整理一下。

import UIKitvar names = []class VIEwController: UIVIEwController,UIAlertVIEwDelegate {    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        var stringValue:String!// stringValue = "chw"// let str11 = stringValue.hashValue// var stringValue:Optional<String>        if let str = stringValue {            let hashValue = str.hashValue        }        let str1 = "swift"        for k in str1 {            println(k)        }        var dic = [0:0, 1:0, 2:0]        var newDic = dic        //Check dic and newDic        dic[0] = 1        println(dic)     //[0: 1,1: 0,2: 0]        println(newDic)//[0: 0,2: 0]        var arr11:Array = [0,"lll",0]        var newArr = arr11        arr11[0] = 1        //Check arr and newArr         println(arr11)    //[1,0]        println(newArr) //[0,0]// 字符串        let heart = "❤️"        println(heart)//❤️        //空字符串        let empty = ""        let anotherEmpty = String()        if empty.isEmpty {            println(" empty ")        } else {            println(" not empty ")        }        //前缀后缀        let str11 = "chengHong wei"        if str11.hasPrefix("ch") {            println("prefix is ch")        }        if str11.hasSuffix("wei") {            println("suffix is wei")        }        //大小写        let up = str11.uppercaseString        println(up)//CHENGHONG WEI        let low = str11.lowercaseString        println(low)//chenghong wei// 可选值探究        var input:String?        var input1 = "18"        var age = input1.toInt()        if age != nil{            println("your age is " + String(age!))        } else {            println("error for input")        }// 遍历数组        let arr = ["1","2","3"]// ...相当于[0,2]一个区间,意思就是 0=< i && i<= 2        for i in 1...arr.count {            println("arr count is \(arr.count)")            println("arr object index \(arr[i-1])")        }// ..<是半闭半开区间,意思就是[0,arr.count) (之前是..,现在已经改了)        for i in 0..<arr.count {            println(i)        }        for var i=0;i<3;i++ {            println("\(arr)")        }// array加元素        var arr1 = ["a"]        arr1 += ["b"]        arr1 += ["c","d","e"]        println(arr1)        println("arr1 is \(arr1)")        arr1[0] = "f"        println(arr1)        var arr20 = [1,2,3,4]        arr20[0] = 0        arr20[1...1] = [5,8]//在array[1]这儿插入了5,8两个元素,这个功能仅在区间状态有效,如果a[3] = [5,8] 则会报错        println(arr20) //[0,5,8,3,4]// 泛型是如何用来定义容器的存储的。还有一个数组的缩写形式,这个更具有可读性,但本质上是一样的。现在你不能往数组里面添加非Int型的元素。这听起来挺糟糕,但它非常有用。再也不需要用API来记录数组里存储了哪些从某个方法返回或者以属性存储的元素。你可以告诉编译器这些信息,编译器在错误检查方面会更加智能,并且可以提早做出优化。        let arr2: [Int] = [1,4] //数组里面只能是int型        let arr3: [float] = [1,2.3]        println("arr3 is \(arr3)")        let arr4: [String] = ["1","chw","love"]        println("arr4 is \(arr4)")// var 可变的数组是可以加元素的,let 声明的是不可加的,也就是可变与不可变数组的意思,在 Objective-C 和 Cocoa 中,您通过选择两个不同的类( Nsstring 和 NSMutableString )来指定该字符串是否可以被修改,Swift中的字符串是否可以修改仅通过定义的是变量还是常量来决定,实现了多种类型可变性 *** 作的统一。        var arr5:Array = [1,3,"cheng"]        arr5.append("chw")        println("arr5 is \(arr5)")// 字典        let dic11 = ["name":"chw","sex":1,"height":175.0]        println("This is a dictionary \(dic11)")// Swift的=是没有返回值的        var a1 = 5        var a2 = 5        if a1 == a2 {            println("===")        } else {// println("!==")        }// 字符串拼接        let cheng = "c"        let hong = "h"        let wei = "w"        let chenghongwei = cheng + hong + wei        println("my name is \(chenghongwei)")// 取余        println(8%3) // = 2        println(8%2.5) // 8 % 2.5 = 8 - 2.5 * 3 = 0.5 小数也是可以取余的// swith语句        var i = 14        switch i {        case 0...10:            println("range is 0-10")        case 11...20:            println("range is 11-20")        case 21..<30:            println("range is [21-30)")        default:            break        }        var name = "chw"        switch name {        case "cheng":            println("name is cheng")        case "hong":            println("name is hong")        case "wei":            println("name is wei")        default:            println("someone else")        break        }// 闭包初探        names = ["Chris","Alex","Ewa","barry","DanIElla"]        func backwards(s1:String,s2:String)->Bool {            return s1>s2        }// UI控件        let lab = UILabel(frame: CGRectMake(100, 100, 200, 100))        lab.text = "Hello Chw"        lab.backgroundcolor = UIcolor.greencolor()        lab.textAlignment = NSTextAlignment.Center        self.vIEw.addSubvIEw(lab)        let btn = UIbutton(frame: CGRect(origin: CGPointMake(100,250),size: CGSizeMake(100,50)))        btn.backgroundcolor = UIcolor.graycolor()        btn.setTitle("Chw btn",forState: UIControlState.normal)        btn.addTarget(self,action: "btnClick:",forControlEvents: UIControlEvents.touchUpInsIDe)        self.vIEw.addSubvIEw(btn)} //Todo:---    func btnClick(sender:UIbutton!){        println("chw 111")        var alert = UIAlertVIEw()        alert.Title = "CHW"        alert.delegate = self        alert.message = " This is a alert !"        alert.addbuttonWithTitle("Cancle")        alert.addbuttonWithTitle("OK")        alert.show()    }//MARK:-//MARK:--- AlertVIEwdelegate    func alertVIEw(alertVIEw: UIAlertVIEw,clickedbuttonAtIndex buttonIndex: Int){        println("buttonIndex:\(buttonIndex)")        if buttonIndex == 0 {           println("Cancle Btn !")        } else if buttonIndex == 1 {           println("Sure Btn")        }    }//MARK:-    overrIDe func dIDReceiveMemoryWarning() {        super.dIDReceiveMemoryWarning()        // dispose of any resources that can be recreated.    }}
总结

以上是内存溢出为你收集整理的一些Swift小知识点的整理全部内容,希望文章能够帮你解决一些Swift小知识点的整理所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1086071.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存