
//(1)var str = "abc"if(str == nil){}//String? 为String可选型,他意味着这种类型,可以为nil,还是那句话,可选型和nil不是一个类型的特殊状态,他是一种新的类型。//2var optionalStr:String? = "hello"optionalStr = "world"print(optionalStr == nil)var greet = "hey"if let name = optionalStr{ greet = "hello \(name)";}print(greet)if(optionalStr != nil){ print("optional str is not nil")}where与模式匹配
//switch中使用where语句let point = (3,3)switch point { case let(x,y) where x == y: print("x == y") case let(x,y) where x == -y: print("x == -y") default: print("rst is \(point.0),\(point.1)")}//switch中使用运算符 let age = 19switch age{ case 10 ... 19: print("teenager") default: print("not teenager")}//if中使用模式 if case 10...19 = age where age > 18{ print("teenager and in colldge")}//if中使用模式和where语句let vector = (4,0)if case (let x,0) = vector where x > 2 && x < 5{ print("it is vector")}//if中使用模式+where+运算符 for case let i in 1 ... 100 where i % 3==0{ print("i is \(i)")}guard
//guard else 防止数据错误,也可以认为是需要满足的一种先决条件//例如下面money>price,capacity > volume是必须满足的条件,简化语法,防止错误func buy(money:Int,price:Int,capacity:Int,volume:Int) -> Bool{ guard money > price else { print("not money") return false } guard capacity > volume else{ print("not volume") return false } return true}@H_301_225@String
//字符串var originStr = "this is str"originStr = "this is new str"//定义一个空字符串let emptyStr = ""//判空 *** 作print(emptyStr.isEmpty)//合并字符串var rstStr = originStr + emptyStr//使用+=,此时rstStr必须是变量rstStr += originStr//字符串插值rstStr = "this is new str and insert \(100)"//转义字符 \//遍历字符串for c in originStr.characters{ print(c)}//Characterlet cc:Character = "!"originStr.append(cc)//字符串长度,基于unicode码,也就是说不管是3个汉字或者3个字母,它的count都是3let englishLetter = "abc"let chinseLetter = "网"let emojiLetter = "������"let unicodeLetter = "\u{1f60e}\u{0301}"print(englishLetter.characters.count)print(chinseLetter.characters.count)print(emojiLetter.characters.count)print("enlish is \(englishLetter) chinse is \(chinseLetter) emoji is \(emojiLetter) unicode is \(unicodeLetter)")//索引访问字符串,需要使用String.Index类来访问//[startIndex,endindex)let startIndex = originStr.startIndexlet endindex = originStr.endindexprint("index is \(startIndex) and content is \(originStr[startIndex])")//向后n个startIndex.advancedBy(6)//前面一个位置endindex.predecessor()//后面一个位置startIndex.successor()//String的一些API//Range<Index>类型let range = startIndex.advancedBy(3) ..< endindexoriginStr.replaceRange(range,with: "!!!")originStr.appendContentsOf("xxx")originStr.insert("z",atIndex: originStr.endindex) originStr.removeAtIndex(originStr.endindex.predecessor()) originStr.removeRange(originStr.endindex.advancedBy(-2)..<originStr.endindex)print("upper \(originStr.uppercaseString) low \(originStr.lowercaseString) First up \(originStr.cAPItalizedString)")originStr.containsstring("")originStr.hasSuffix("")originStr.hasPrefix("")//Nsstring let s2 = Nsstring(format: "one third is %.2f",1.0/3.0)print("转换 \(s2 as String)")let nsstr:Nsstring = "one third is 0.33"nsstr.substringFromIndex(4)nsstr.substringFromIndex(3)nsstr.substringWithRange(NSMakeRange(4,5))let s6 = " --- Hello --- " as Nsstring//去掉空格和-print(s6.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString:" -"))总结
以上是内存溢出为你收集整理的Swift基础进阶全部内容,希望文章能够帮你解决Swift基础进阶所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)