目录:
1.playground
2.常量和变量
3.数据类型
4.swift中的字符串
5.元祖数据类型
6.nil的应用
7.类型别名:自定义类型
8.数据转换基础
//1.playground
//: Playground - noun: a place where people can play
本示例都用Playground调试,用xcode的playground来学习基础语法,不用编译方便调试并查看结果。
新建Playgroud:File->New->Playground
//2.常量和变量
let para_constant ="常量";
var para_var ="变量";
print(para_constant);
print("par1=\(para_constant),par1=\(para_var)");
//3.数据类型
var varDouble =3.454;//默认double
var varFloat:Float =3.4;//定义float
var varInt =234;//默认int ,64位系统等于int64,32位是int32
var varInt32:Int32 =343434;//Int32
var varDemicalInt =23;//十进制
var varBinaryInt =0b1010;//二进制
var varOctalInt =0o45;//八进制
var varHexInt =0x3298;//16进制
var varIntBigData =1_000_000;//增加下划线,增加可读性
var varBool:Bool =true;
if(varBool)
{
print(varIntBigData);
}
else
{
print("false");
}
//4.swift中的字符串
var varStr1 ="";
let varStr ="ert";
let upperStr =varStr.uppercaseString;//大写
let lowerStr =upperStr.lowercaseString;//小写
print("\0");//转义字符:\
let varUnicode1 ="\u{41}";//单字节Unicode
let varUnicode2 ="\u{2600}";//双字节Unicode
let varUnicode4 ="\u{0001F496}";//4字节Unicode
if(varUnicode1==varUnicode2)
{
print("equal!")
}
else
{
print("unequal");
}
let varCon =varUnicode1+varUnicode2;//连接字符串:+ +=
for chinvarStr.characters//遍历字符串输出单个字符
{
print(ch);
}
print(varStr.characters.count);//统计字符串字符个数
for chinvarStr.unicodeScalars//输出Unicode编码
{
print(ch.value);
}
//5.元祖数据类型
let letGroup1 = ("fly",123,1.2);
let letValueInt =9994;
let letValueStr ="letStr0";
let letGroup2 = (letValueInt,letValueStr,12670);
let (let1,let2,let3) =letGroup2;
print(let1);
print(let2);
print(let3);
let (_,let4,_) =letGroup1;
print(let4);
let letGroup3 = (para1:123,para2:"dfg",para3:4.5);
print(letGroup3.para2);
//6.nil的应用
var varInt1:Int? =nil;//赋值为空
print(varInt1);
varInt1 =5566;
var varBool1:Bool?;//默认为nil
print(varBool1);
//7.类型别名:自定义类型
typealias myInt =Int;//类似C语言typedef
var varint3:myInt =345;
//8.数据转换基础
var varStr3 ="1234";
var varInt4:Int? =Int(varStr3);
var varStr4 ="ab1234";
var varInt5:Int? =Int(varStr4);
var varInt6 =445454;
var varStr5 =String(varInt6);