数组字典结构体枚举

数组

var GPLarr  = [1,2,3];

var emptyArr  = [String]();//声明一个空数组,字符串类型
emptyArr.append("11111");

var emptyArr2 = [Int]();//int类型

emptyArr2.append(1);

GPLarr.insert(4, at: 3);//指定位置添加元素

print("GPLarr==============\(GPLarr)");

GPLarr[0] = 3;

print("GPLarr==============\(GPLarr)");
> Swift会自动将已确定的数字类型Int和Float转换为NSNumber。这样的转换允许你基于其中一种类型创建一个NSNumber:

let n = 1;

//直接插入数组

GPLarr.insert(n, at: 4);
print("GPLarr==============\(GPLarr)");

print("GPLarr.count===========\(GPLarr.count)");

if GPLarr.isEmpty {

}

else

{

print("不为空");

}
**for循环数组**

for i in GPLarr {

print("i============\(i)");

}
**通过enumerate函数同时遍历数组的所有索引与数据**

for (index,value) in GPLarr.enumerated() {

print("index=======\(index),value===========\(value)");

}

字典

//var dicName:[key类型 : 值类型]

var GplempDic = [String:String]();

var GPLDic = ["key1":"value1","key2":"value2"];

print("\(GPLDic.values)");//字典的key

print("\(GPLDic.keys)");//字典的value

for (key,value) in GPLDic {

print("\(key):\(value)");

}

结构体

struct student

{

var ID = 1;

var NUM : Int = 1;

var name : String = "小明";

}

var student2 = student(ID: 3, NUM: 4, name: "许许多多");

student2.ID = 5;

枚举

enum compasspoint

{

case North

case Sourth

case East

case West

}

你可能感兴趣的:(数组字典结构体枚举)