Swift的变量/常量定义比C/C++/Delphi宽松得多,会随着初始化的值类型而定,当然可以用:type的方式指定数据类型,XCode非常智能,任何常量/变量未初始化,编译将不通过,将这 方面的失误堵截在运行前.
如:
var a:String;
NSLog("a:%@",a); //编译是不能通过的.
定义变量用var,定义常量用let,而delphi 则用var 和const,定义变量可以在变量后面加:type来指定变量数据类型,如 var num:Int32=1234,delphi也是这样的 ^_^
一般的string用法
下面是更详细的string用法(转载)
下面是转载,但swift3.0未须适用.
- 作者:Mandarava(鳗驼螺)
版本2:增加了Swift 2.0的语法,与Swift 1.2的语法相比,主要是:advance方法变成了advancedBy方法(但不是简单替换);没有了count方法,count(str)需要变成str.characters.count等。
- 字符串的定义
var str1="hello,mandarava." @H_404_41@//字符串变量 let str2=//字符串常量 let str3="" @H_404_41@//空字符串 let str4=@H_403_55@String() @H_404_41@//空字符串
- 字符的定义
var char1:Character="m" @H_404_41@//字符变量 let char2:Character=//字符常量
- 字符串的连接
let str1= "mandarava." let str3=str1+str2 @H_404_41@//=连接str1、str2 @H_404_41@//str3="hello,mandarava." @H_404_41@//---------------------------------------- let str4="\(str1)\(str2)" @H_404_41@//str4="hello,0)">let strArray=["apple","orange",152)">"cherry"] let joinStr=",".join(strArray) @H_404_41@//joinStr="apple,orange,cherry"
- 字符串与字符的连接
let char1:Character="o" "hell" let str2=str1+String(char1) @H_404_41@//str2="hello" \(char1)" @H_404_41@//str3="hello" @H_404_41@//---------------------------------------- str1.append(char1) @H_404_41@//str1="hello"
- 字符串与其它类型值的连接
let xx=10 let yy=10.8 "\(xx)+\(yy)=?" @H_404_41@//str1="10+10.8=?" let str2=String(format: "%i+%.1f=?",xx,yy) @H_404_41@//str2="10+10.8=?"
- 字符串枚举字符
@H_404_41@//Swift 1.2 let str="mandarava" for ch in str{ println(ch) } @H_404_41@//----------------------- @H_404_41@//Swift 2.0 str.characters{ print(ch) }
- 获取字符串中指定索引处的字符
let str="Mandarava" chr=str[advance(str.startIndex,2)] @H_404_41@//Swift 1.2 //chr:Character="n" str[str.startIndex.advancedBy(//Swift 2.0 //chr:Character="n"
- 计算字符串长度length
"@鳗驼螺" let len1=count(str) @H_404_41@//swift 1.2 //=4 let len2=str.characters.count @H_404_41@//swift 2.0 //=4 let blen=str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) @H_404_41@//=10
- 字符串比较
let str1="hello,0)">let str2="mandarava." let str3="hello,mandarava." let comp1 = str1==str2 //false let comp2 = str1+str2 == str3 //true let comp3 = str1 < str2 //true let comp4 = str1 != str2 //true
- 是否包含子串contains
let range=str1.rangeOfString(str2) if range != nil{ //包含 }
- 查找子串indexof
var "hi,Mandarava." let range=str.rangeOfString("Mandarava",options: NSStringCompareOptions.allZeros) @H_404_41@//Swift 1.2 404_41@//Swift 2.0 let startIndex=range?.startIndex @H_404_41@//=3
- 首字母大写capitalized
var str1="mandarava is a flower." str1.capitalizedString @H_404_41@//str1="Mandarava Is A Flower.
- 转换为大写字母uppercase
str1=str1.uppercaseString @H_404_41@//str1="HELLO,MANDARAVA."
- 转换为小写字母lowercase
"HELLO,MANDARAVA." str1=str1.lowercaseString @H_404_41@//str1="hello,mandarava."
- 截取字符串substring
let str2=str1.substringFromIndex(advance(str1.startIndex,152)">6)) let str2=str1.substringFromIndex(str1.startIndex.advancedBy(//Swift 2.0 @H_404_41@//str2="mandarava." let str3=str1.substringToIndex(advance(str1.startIndex,152)">5)) let str3=str1.substringToIndex(str1.startIndex.advancedBy(let range=Range<String.Index>(start: advance(str1.startIndex,152)">6),end: advance(str1.endIndex,152)">-1)) String.Index>(start: str1.startIndex.advancedBy(end: str1.endIndex.advancedBy(let str4=str1.substringWithRange(range) @H_404_41@//str4="mandarava"
- 字符串修剪trim
let str1=" mandarava.\n " 2=str1.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) 3=str1.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) @H_404_41@//str3="mandarava.\n" let charset=NSCharacterSet(charactersInString:" \n") 4=str1.stringByTrimmingCharactersInSet(charset) @H_404_41@//str4="mandarava."
- 字符串的分解子串split @H_909_403@"boy,girl,man,woman" let str1Array=str1.componentsSeparatedByString() //str1Array=["boy",152)">" girl",152)">" man",152)">" woman"] var str2= ) let str2Array=str2.componentsSeparatedByCharactersInSet(charset) //str2Array=["girl",152)">"man",152)">"10",152)">"20",152)">"30"]
- 字符串替换replace
var str1="My name is Mandarava." let subRange=Range(start: str1.startIndex,152)">end: advance(str1.startIndex,152)">2)) @H_404_41@//Swift 1.2 let subRange=Range(end: str1.startIndex.advancedBy(//Swift 2.0 str1.replaceRange(subRange,152)">with: "Your") @H_404_41@//str1="Your name is Mandarava." var str2= str2=str2.stringByReplacingOccurrencesOfString(withString: "鳗驼螺") @H_404_41@//str2="hello,鳗驼螺." str2=str2.stringByReplacingOccurrencesOfString("鳗驼螺",152)">options: NSStringCompareOptions.CaseInsensitiveSearch,152)">range: nil)
- string转换为Int/Long/Float/Double/Bool等
"100" var i=str1.toInt()! @H_404_41@//Swift 1.2 //=100 var i=(str1 as NSString).integerValue @H_404_41@//Swift 2.0 //=100 var i=(str1 as NSString).intValue @H_404_41@//=100 var l=(str1 as NSString).longLongValue @H_404_41@//=100 var str2="10.8" var f=(str2 as NSString).floatValue @H_404_41@//=10.8 var d=(str2 as NSString).doubleValue @H_404_41@//=10.8 var str3="true" var b=(str3 as NSString).boolValue @H_404_41@//=true