swift 2.0 改变了一些地方,让swift变得更加完善,这里是一些最基本的初学者的代码,里面涉及到swift学习的最基本的字符串,数组,字典和相关的操作。好了直接看代码吧。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
addString()
addArray()
Arraytest()
Dictorytest()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
func setbutton( composedButton:UIButton )
{
composedButton.frame = CGRectMake(100, 100, 100, 100)
composedButton.backgroundColor=UIColor.redColor()
}
//====================== 字符串============================
func addString ()
{
// 字符串与字符的拼接
let Z : Character = "B"
let zx : String = "asdasdafdaf"
let zxzx = zx + String(Z)
// 字符串与其他类型的拼接
let a = 10
let zzxzx = String(format: "%d %@", a ,zxzx)
print(zzxzx)
// 字符串的遍历
for ch in zxzx.characters
{
print(ch)
}
// 获取制定处的字符串
let c = zzxzx[zxzx.startIndex.advancedBy(3)]
print("=============",c)
// 指定位置处天际字符串
var str = "zxczcbzbcjb"
str.insert(c, atIndex: str.startIndex.advancedBy(4))
print(str)
// 计算字符串的长度
let b = str.characters.count
print(b)
// 截取字符串 注意这里的 -3 或者截取到 -1 。这里的意思是。-3 就是从最后一位开始向前倒数三位 -1 就是从最后一位开始倒数1位
let rang = Range<String.Index>(start: str.startIndex.advancedBy(2), end: str.endIndex.advancedBy(-3))
let m = str . substringWithRange(rang)
print("---------------",m)
// 字符串的替换
var String1 = "woxihuanni我喜欢你"
String1 = String1.stringByReplacingOccurrencesOfString("我喜欢你", withString: "c", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
String1 = String1.stringByReplacingOccurrencesOfString("c", withString: "我")
print(String1)
// 字符串类型的装换
let y = "1000"
let f = (y as NSString).integerValue;
print(f/10)
// 基本类型求余
let j = 28.4
print(j%0)// nan 是非数
}
// ============================== 数组 ==============================
func addArray()
{
var ZXArray:[String] = ["zhangxu" , "zhangxu1" ,"zahngxu2"]
// 增加
ZXArray.insert("cxc", atIndex: 2)
// 删除
ZXArray.removeAtIndex(2)
// 数目
print(ZXArray.count)
// 遍历
for zhangxu in ZXArray
{
print(zhangxu)
}
print(ZXArray)
}
func Arraytest()
{
var zarray :[[Int]] = [[1,2,3],[4,5,6],[7,8,9]]
var xarray = [[Int]]()
xarray=zarray;
for var i = 0; i<3; i++
{
for var j=0 ; j<3; j++
{
print(zarray[i][j])
}
}
for var i = 0; i<3; i++
{
for var j=0 ; j<3; j++
{
xarray[i][j]=0
}
}
print(xarray)
var zxcarray:[[Int]]
zxcarray = []// 空数组
zxcarray.insert(xarray[0], atIndex: 0)
print(zxcarray)
}
//=============================== 字典 ================================================
func Dictorytest()
{
// 字典的声明和创建
var dictest:[String:String]
dictest=["123":"ren"]
dictest.updateValue("zhangxu", forKey: "ren")
print(dictest)
print(dictest.isEmpty)
var zdictest:[String:Int]=[:]
print(zdictest.isEmpty)
// 字典内容的增加
zdictest.updateValue(1,forKey: "zhangxu2")
print(zdictest.isEmpty)
var zx = zdictest["zhangxu2"]
print(zx!)
// 字典的遍历
for(key,value) in dictest
{
print(key,value)
}
// 字典内容的删除
dictest.removeValueForKey("123")
print(dictest)
// 修改字典里面 ren 这个KEY 对应的值
dictest.updateValue("zhanxgu3", forKey: "ren")
print(dictest)
}