swift中文文档gitbooks地址:https://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/
1、基本运算符
其实无论什么语言,对于基本运算符来说,都是差不多的,这里我就说下swift的区别其他语言的地方。例如,范围运算符,swift分为封闭范围运算符和半封闭范围运算符,
封闭范围运算符(a…b)定义了一个范围,从a到b,并包括a和b的值。当要在一个范围内迭代所有可能的值的时候,范围运算符是非常有用的, 例如for-in循环
[objc] view plain copy
- for index in 1...5
- {
-
-
- println("index:\(index)");
-
- }
打印的结果:
index:1
index:2
index:3
index:4
index:5
半封闭的区域运算符(a..b)定义了从a到b的范围,但不包括b。它被认为是半封闭的,因为它包含第一个值,而不包含最终值。半封闭的范围使用明确,当你使用从零开始的列表,如数组,它是有用的数到(但不包括)列表的长度:
[objc] view plain copy
- let objects=["a","b","c","d"];
-
- for i in 0..objects.count
- {
-
-
-
- println("index:\(objects[i])")
-
- }
打印结果:
index:a
index:b
index:c
index:d
2、字符串和字符
如下:
[objc] view plain copy
-
- let string0="I'm String";
-
-
- var string1="";
- var string2=String();
-
-
- if string1.isEmpty
- {
-
- println("empty");
-
- }
-
-
- var string3="Hello";
- string3+="World";
- println(string3);
-
-
-
- for character in string3
- {
-
-
- println(character);
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- let charo:Character="a";
-
- let numsChar="Hello,Da Yu Yu";
-
- println("nums is \(countElements(numsChar))");
-
-
-
-
-
-
-
-
- let string4="ni";
- let string5="hao";
- let character1: Character = "!"
- let character2: Character = "?"
-
- let newString1=string4+character1;
- let newString2=string4+string5;
- let newString3=character1+string4;
- let newString4=character1+character2;
-
-
-
- let string6="a";
- let string7="b";
-
- if a==b
- {
-
- println("same");
-
- }
-
- let string8="abcdefgh";
-
- if string8.hasPrefix("a")
- {
-
-
- println("yes");
-
- }
-
- if string8.hasSuffix("h")
- {
-
-
- println("yes");
-
- }
-
-
- let string9="Hello,World";
-
- let string10=string9.uppercaseString;
-
- println(string10);
-
- let string11=string9.lowercaseString;
-
- println(string11);