swift 字符串链接、数组、字典 增删改查 遍历等等 学习记录
这文章笔者 创建osx 系统应用程序去学习
例子:
// // main.swift // osxProject01 // // Created by hins on 16/10/25. // Copyright © 2016年 hins. All rights reserved. // import Foundation print("Hello,World!") //=================字符串链接 var str = "hello "; var wall = str + "redhat"; print(wall); var yiyong = "\("women"),age is,\(100)"; print(yiyong); var num = 25; var m = "man"; var strTest = "james age is \(num) a \(m)"; print(strTest) //=====================数组(增删改查) //这里只介绍增删改查,其余的可以查看相关的API: 例如:数组长度(个数),排序,是否含有某个值等等; var array = ["xuan","man",200,1/3,2.3]; var nullArr = [];//空数组 var strArray:[String];//指定存放类型的数组 strArray = ["h","w","2.1"]; var intArr:[Int] = [1,2,3,9]; //泛型 var arr3 = Array<String>(); arr3 = ["222","4455"]; print(arr3[1]);//下标取值 //向数组追加元素 arr3.append("666"); arr3.insert("7777",atIndex: 0);//在下标为0的位置insert arr3.insert("9999",atIndex: 1);//在下标为1的位置insert arr3.count; //删除元素 arr3.removeAtIndex(0); arr3.removeLast(); print(arr3); //数组遍历方式1 for item in arr3{ print("===="+item) } //数组遍历方式2 for (index,value) in EnumerateSequence(arr3){ print("\(index+1)======="+value); } //=====================字典 key value(相当于Java的Map集合) (增删改查) //定义 var dict = ["name":"xuan","age":24,"email":"991433544@qq.com"]; dict["address"] = "广州";//追加 dict["age"] = 56;//修改 也可以用updateValue(); dict.updateValue("深圳",forKey: "address"); print(dict["address"]);//取值 print(dict.count); dict["name"]=nil;//删除 dict.removeValueForKey("age");//删除 print(dict); //泛型 var dictInt = Dictionary<Int,String>() var dict1:Dictionary<String,String> = Dictionary(); var dict2:Dictionary<String,String> = Dictionary<String,String>(); dict1["name"]="haha"; dict1["age"]="34"; print(dict1); dict2["name"]="xixi"; dict2["age"]="56"; print(dict2); //字典遍历方式1 for (key,value) in dict{ print("====1==\(key),\(value)"); } for value in dict.keys{ print("=======2=======\(value)") } for value in dict.values{ print("=======3=======\(value)") }
创建osx 应用程序步骤(如图,下一步按下去就行了,不同于palyground应用的实时运行):
osx 应用程序步骤需要手动运行,控制台打印输出
1创建