• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift系统学习第一章

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

第一节:变量,常量,类型推断,字符,字符串

 1 //swift学习第一节
 2 /*
 3 变量
 4 常量
 5 类型推断
 6 字符
 7 字符串
 8 */
 9 
10 import UIKit
11 
12 //变量
13 var str = "swift"   //类型推断
14 str = "hello"
15 var str1:NSString =  "shaoting"
16 var num = 123
17 var num2:Int = 123
18 
19 //常量
20 let num3 = 321     //类型推断
21 //num3 = 123  error:常量不可以修改
22 let str2:NSString = "pheromone"
23 
24 //Int 有符号的整形
25 //UInt 无符号的整形
26 
27 Int.max
28 Int.min
29 
30 var numFloat : Float = 1/3
31 var numDouble : Double = 1/3
32 
33 var num4 = 3
34 num4.dynamicType  //类型
35 var num5 = 0.14
36 num5.dynamicType
37 
38 num5 = 3 + 0.14
39 num5.dynamicType
40 //num5 = num4 + num5 eroor:类型不同不能相加
41 num5 = Double(num4) + num5
42 
43 //初始化字符串,默认值为nil
44 let strr = ""
45 let strrr = String()
46 
47 let swift:Character = "S"
48 let swiftInChar:[Character] = ["s","w","i","f","t"];
49 let swift1 = String(swiftInChar)
50 let swift11 = swiftInChar
51 
52 var message = "i love you"
53 let meessageLength = message.characters.count
54 
55 //message[0]  error:swift中,访问字符串中的某个字符不能使用字符串数组的某个下标
56 message.startIndex  // 第一个下标
57 message.endIndex    //最后一个下标
58 message[message.startIndex.successor()] //第一个下标的下一个值
59 message[message.endIndex.predecessor()] //最后一个下标的上一个值
60 message[message.startIndex.advancedBy(3)]
61 
62 message.insertContentsOf("iOS".characters, at: message.startIndex)//在某个字符串的某个位置加入某些字符
63 message.removeAtIndex(message.endIndex.predecessor())
64 //message.removeRange(<#T##subRange: Range<Index>##Range<Index>#>)  删除范围内的内容
65 
66 //比较字符串
67 let message1 = "ios hello"
68 if message != message1{
69    print("!=")
70 }
71 
72 let swi = "我是一只猿"
73 swi.hasPrefix("")//判断前端是否包含
74 swi.hasSuffix("")//判断后端是否包含
75 
76 for code in swi.utf8 {
77    print("\(code)")
78 }
79 
80 //数组
81 var strS:[String] = ["ios","swift"];
82 //var strS1:[] = [12,34];  没有指定元素类型

第二节:元组Tuple 操作符operators

 1 /*
 2 swift学习第二节
 3 元组 Tuple
 4 操作符 Operators
 5 
 6 */
 7 
 8 
 9 import UIKit
10 
11 //元组  :自己定义一组数据,类型也自己定义
12 let success = (200,"HTTP OK")
13 let fileNotFound = (404,"File not found")
14 let me = (name:"shaoting",no:520,email:"[email protected]")
15 success.0
16 success.1
17 me.name
18 
19 var (code,message) = success
20 print(code)
21 print(message)
22 
23 
24 //操作符 Operators
25 var a = 10
26 var b = 20
27 
28 let sum = a + b
29 let sub = a - b
30 let mul = a * b
31 let div = a / b
32 let mod = a % b
33 let mod1 = 8 % 2
34 
35 b += 10 // b = b+10
36 b -= 10 // b = b-10
37 b *= 10 // b = b*10
38 b /= 10 // b = b/10
39 b %= 10 // b = b%10
40 
41 let isEqual = sum == 10
42 isEqual ? "YES" : "NO"
43 
44  //区间运算符
45 //包含1到5
46 for index in 1...5{
47   print(index)  // 1 2 3 4 5
48 }
49 
50 for index in 1..<5{
51   print(index)  // 1 2 3 4
52 }

第三节:集合--有序集合(数组)无序集合(set,dictionary)

  1 /*
  2 swift学习第三节
  3 集合(数组,set,dictionary)
  4    有序集合: 数组
  5    无序集合: set dictionary
  6 */
  7 
  8 
  9 
 10 import UIKit
 11 //数组
 12 var ary1:Array<Int> = Array<Int>()//定义一个值为空的int类型的数组
 13 var ary2:[Int] = ary1
 14 var ary3 = ary1
 15 
 16 var three = [Int](count: 3, repeatedValue: 1)
 17 var six = three + three   //相同类型的数组可以做运算
 18 
 19 var four = [1,2,3,4]
 20 four.count
 21 
 22 if ary1.isEmpty{
 23   print("ary1 为空数组")
 24 }
 25 
 26 ary1.append(1) // 添加元素
 27 ary1 += [2,3,4] // 添加多个
 28 ary1[0...2]
 29 ary1[0..<2]
 30 ary1[0...1] = [5,6]  //修改元素
 31 ary1
 32 ary1.insert(9, atIndex: 1) //在某个下标添加元素
 33 ary1.removeAtIndex(2)  //删除某下标的元素
 34 ary1.removeFirst()   //删除第一个元素
 35 ary1.removeLast()    //删除最后一个元素
 36 ary1
 37 for (index,value) in ary1.enumerate(){
 38   print("index : \(index) value : \(value)")
 39 }
 40 
 41 //set
 42 //输出hash值
 43 let number = 10
 44 number.hashValue
 45 let PI = 3.14
 46 PI.hashValue
 47 let string = "Swift"
 48 string.hashValue
 49 
 50 //定义一个元素类型为字符的空set
 51 let emptySet = Set<Character>()
 52 let vomel:Set<Character> = ["a","e","i","o","u"];
 53 var evenSet:Set = [2,4,6,8,10] //类型推断
 54 evenSet.count   //该set的个数
 55 evenSet.removeFirst()   //移除set的第一个元素
 56 evenSet.insert(25)      //插入一个元素25
 57 evenSet.contains(4)    //判断是否包含
 58 //因为set是无序集合,想要保证一定顺序可以使用sort
 59 for number in evenSet.sort(){
 60    print(number)
 61 }
 62 
 63 //setA 和 setB 的交集
 64 var setA : Set = [1,2,3,4,5]
 65 var setB : Set = [5,6,7,8]
 66 let setiAB : Set = setA.intersect(setB)
 67 //setA 和 setB 的对称差
 68 let  seteAB : Set = setA.exclusiveOr(setB)
 69 //setA 和 setB 的并集
 70 let unionAB : Set = setA.union(setB)
 71 //setA对于setB的补集
 72 let subAB : Set = setA.subtract(setB)
 73 
 74 //判断集合是否相等
 75 if setA == setB{
 76   print("相等")
 77 }else{
 78   print("不相等")
 79 }
 80 
 81 setB.isSupersetOf(setA) //判断B是否是A的超集
 82 setB.isSubsetOf(setA)//判断B是否是A的子集
 83 
 84 
 85 //字典
 86 //创建一个key是int value是string的字典
 87 var int2String = [Int:String]()
 88 int2String = [:]
 89 int2String[10] = "Ten"
 90 int2String[20] = "Twenty"
 91 
 92 var capitalNumber = [
 93     1:"",
 94     2:"",
 95     3:""
 96 ]
 97 
 98 capitalNumber[1]  // 字典某个下标值
 99 capitalNumber.count //字典的个数
100 capitalNumber.isEmpty //判断字典是否为空
101 capitalNumber.updateValue("yi", forKey: 1) //修改字典某个key对应的值
102 capitalNumber[2] = nil   //对某个下标置空
103 capitalNumber.removeValueForKey(2) //移除某个key对应的值
104 capitalNumber
105 capitalNumber.dynamicType  //类型
106 
107 for (key,value) in capitalNumber{
108    print("\(key):\(value)")
109 }
110 let keyAry = [Int](capitalNumber.keys);
111 let valueAry = [String](capitalNumber.values);

第四节:循环语句(for while repeat-while switch)

  1 /*
  2 swift学习第四节
  3 循环语句
  4 for  while  repeat-while  switch
  5 
  6 */
  7 import UIKit
  8 
  9 for var i = 0;i < 10; i++ {
 10    print(i)
 11 }
 12 
 13 let vomel = ["a","e","i","o","u"]
 14 for char in vomel{
 15     print(char)
 16 }
 17 
 18 for number in 1...100{
 19    print(number)
 20 }
 21 
 22 for _ in 1...10{
 23     print("*",terminator:"")
 24 }
 25 
 26 var i = 0
 27 while i < 10 {
 28    print(i)
 29    ++i
 30 }
 31 
 32 //var n = 0
 33 //repeat {
 34 //    print(vomel[n])
 35 //     ++i
 36 //}while n < 5
 37 
 38 let me = "a"
 39 switch me{
 40 case "a":
 41     print("me is \(me)")
 42 case "e":
 43     print("me is \(me)")
 44 case "i":
 45     print("me is \(me)")
 46 case "o":
 47     print("me is \(me)")
 48 case "y":
 49     print("me is \(me)")
 50 default:
 51     print("都不是")
 52 }
 53 
 54 switch me{
 55 case "a","e","i","o","u":
 56     print("这个是元音")
 57 default:
 58     print("这个不是元音")
 59 }
 60 
 61 let number = 56
 62 switch number{
 63 case 1...10:
 64     print("这个数在1-10之间")
 65 case 11...99:
 66     print("这个数在11-99之间")
 67 default:
 68     print("这个数大于100")
 69 }
 70 
 71 let point = (1,1)
 72 switch point{
 73 case (0,0):
 74     print("在原点")
 75 case (_,0):
 76     print("在x轴上")
 77 case (0,_):
 78     print("在y轴上")
 79 case (-2...2,-2...2):
 80     print("在该范围内")
 81 default:
 82     print("位置跑偏")
 83 }
 84 
 85 switch point{
 86 case (let x,0):
 87     print("\(x)")
 88 case (0,let y):
 89     print("\(y)")
 90 case let(x,y):
 91     print("\(x),\(y)")
 92 }
 93 
 94 switch point{
 95 case let(x,y) where x == y:
 96     print("X=Y")
 97 case let(x,y) where x == -y:
 98     print("x=-y")
 99 case let(x,y):
100     print("\(x),\(y)")
101 
102 }

源码下载地址:

http://download.csdn.net/detail/shaoting19910730/9461377

https://github.com/pheromone/swift-

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift 自定义分段控制器和使用发布时间:2022-07-13
下一篇:
Swift使用SDWebImage处理远程图片资源发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap