前端之家收集整理的这篇文章主要介绍了
Swift基础进阶,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Swift基础进阶
可选型的概念
数据类型+?将会构成一种新的类型,可选型,String?为字符串可选型,,在swift中,空的概念略有不同,java中,如果一个对象变量,没有指向,为null;一个int类型的变量如果没有初始化,值为0,null和0 意为没有,在swift中,空是一种完全独立的数据类型,nil的位置和Int,String的地位是一样的,他不是对象或者数据的一种特殊状态,而是一种新的对象。这也就意味着代码中(1)的部分是编译错误的,会提示,nil类型是不能和String类型进行的比较的,该设计的目的是为了使swift更具安全性,因为任何对象都不能设置为nil,就如同String类型的数据不能设置为int类型的数据,他们是完全不同的对象,可以避免潜在空指针的发生。
var str = "abc"
if(str == nil){
}
var 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与模式匹配
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)")
}
let age = 19
switch age{
case 10 ... 19:
print("teenager")
default:
print("not teenager")
}
if case 10...19 = age where age > 18{
print("teenager and in colldge")
}
let vector = (4,0)
if case (let x,0) = vector where x > 2 && x < 5{
print("it is vector")
}
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
}
String
var originStr = "this is str"
originStr = "this is new str"
let emptyStr = ""
print(emptyStr.isEmpty)
var rstStr = originStr + emptyStr
rstStr += originStr
rstStr = "this is new str and insert \(100)"
for c in originStr.characters{
print(c)
}
let cc:Character = "!"
originStr.append(cc)
let 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)")
let startIndex = originStr.startIndex
let endIndex = originStr.endIndex
print("index is \(startIndex) and content is \(originStr[startIndex])")
startIndex.advancedBy(6)
endIndex.predecessor()
startIndex.successor()
let range = startIndex.advancedBy(3) ..< endIndex
originStr.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("")
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:" -"))
原文链接:https://www.f2er.com/swift/323894.html