swift的数组和字典使用
//
// ViewController.swift
// swift基本语法004
//
// Created by Ninesday on 16/6/22.
// Copyright © 2016年 Ninesday. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// demo()
// demo1()
// demo2()
// demo3()
// demo4()
// demo5()
//demo6()
//demo7()
// arrayTogether()
// demo8()
// dictDemo()
// demodict()
// demoList()
demoTogether()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//字符串的操作
func demo() {
let str = "hello world"
let s1 = (str as NSString).substringWithRange(NSMakeRange(2, 4))
print(s1)
}
//循环操作
//方法1
func demo1() {
for var i = 0; i<10; i++ {
print(i)
}
}
//方法2:
func demo2() {
for i in 0..<9 {
print(i)
}
}
//方法3:
func demo3() {
for i in 0...9 {
print(i)
let a = 0..<20
print(a)
let b = 0...20
print(b)
}
}
//数组演练
//结构体可以打印 结构体需包装成 NSValue
func demo4() {
let array = ["zhangsan ","lisi"]
let array2 = [ "zhangsan", "lisi", 18, NSValue(CGPoint:CGPoint(x: 20, y: 20))]
print(array)
print(array2)
}
//数组可变和不可变 可变用var 不可变用let
//在定义数组的时候会制动推导为 String 如果你想追加的时候一般默认追加为string
//如果非要追加为Int 类型的话 你可以 在定义自恒歌数组的时候就声明为 var array:[NSObject] =
func demo5() {
var array = ["zhangsan","lisi",]
//追加对象
array.append("wangwu")
print(array)
//如果要追加Int类型的
var array1:[NSObject] = ["huzuyu","hansome"]
array1.append(19)
print(array1)
//AnyObject swift新语法 表示任意的对象 swift对象没有任何父类
}
//数组的基本操作
//1.数组的遍历
func demo6() {
let array = ["zhnagsan","lisi"]
for s in array {
print(s)
}
}
//数组的修改 删除 添加
func demo7() {
var array = ["zhangsan","lisi"]
array.append("haha")
//修改
array[0] = "zhangsanwoaini"
//删除
print(array)
array.removeFirst()
// array.removeLast()
print(array)
}
//数组的合并
//陷阱 如果加上一个的 如何不同的类型都要非常清楚
func arrayTogether() {
var array1 = ["张三","lisi"]
var array2 = ["wangwu","zhaoliu"]
array1 += array2
print(array1)
var array3:[NSObject] = ["lssaf","lisi",18]
var array4:[NSObject] = ["dsfhfdsf",10]
array3 += array4
print(array3)
}
//关于数组的容量测试
//使用可变的数组的时候 如果知道数组的容量 应该使用capacity实例化数组 一次性分配空间
func demo8() {
var array:[String]
//分配空间
array = [String]()
for i in 0...16 {
array.append("huzuyu\(i)")
print(array[i] + "容量\(array.capacity)")
}
}
//MARK:-字典的演练
//OC中用{}swift中任用[] 是通过值对
func dictDemo() {
let dict = ["name":"huzuyu","age":18]
print(dict)
}
//从网络获取的JSON要求 那个key必须是NSString
//顶级节点必须是 NSString NSNumber NSArray NSDictionary
//JSON中字符串和NSNumber怎么区别 NSNumber没有引号
//字典可变和不可变
func demodict() {
var dict = ["name":"yangyan","age":18]
//key值存在的话 就覆盖
dict["name"] = "huzuyu"
//如果key值不存在的话就添加进去
dict["title"] = "I'm changing"
print(dict)
}
//MARK:-字典的遍历
func demoList() {
var dict = ["name":"huzuyu","age":24,"love":"self","parents":"hfsdfas"]
for(ki, vi) in dict {
print("KEY \(ki)----------VALUES \(vi)")
}
}
//MARK:-字典的合并
//key有的话就覆盖
func demoTogether() {
var dict = ["name":"huhaha","age":24, "love":"nafa"]
var dict2 = ["name":"huzuyu","sex":"female"]
for (k,v) in dict2{
dict[k] = v
}
print(dict)
}
}