Swift自学_数据类型

前端之家收集整理的这篇文章主要介绍了Swift自学_数据类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Var 定义变量

Let 定义常量

printf() 打印

var num = 1 //int变量

var num = 10.0 //double 变量


字符串

NSString ---> String "String" 直接双引号 可以像脚本语言一样直接对字符串想加减,可以带入变量

let c = "cap"

"a"+"b"+c = "abcap"

let d = "I have \(c) apples"// \(c)将常量c填充入字符串,类似format、


数组

NSArray --->声明空数组的话使用[] 或者let emptyArray = [String]()

let arr = ["a","b","c"]

NSMutableArray--->

var arr = ["a","c"]

arr[0] = "apple" //替换元素


字典

NSDictionary--->声明空字典的话使用[:] 或者let emptyDictionary = [String: Float]()

let dic = ["keyA":"A","keyB":"b","keyC":"c"]

NSMutableDictionary--->

var dic = ["keyA":"A","keyC":"c"]

//addobject 添加

dic["KeyD"]="D" //相当于字典addobject

//setobject 设置

@H_403_130@ dic["D"] = "Dan"

@H_403_130@ print("dic=",dic)

@H_403_130@

//removeObjectForKey 移除某个key和对应的键值

@H_403_130@ dic["D"] = nil

//removeAllobject 移除所有

@H_403_130@ dic = [:]

@H_403_130@ print("dic)

原文链接:https://www.f2er.com/swift/325404.html

猜你在找的Swift相关文章