Swift 学习笔记(基本类型)

直接上代码,不BB。。用 playground编写!
import UIKit
//
var str = "Hello, playground"
//开发语言,面向对象
/*1.let 常量,这个不要点,我不知道是哪里来的,;不要结束符号,我不知道是那种语言

/
let string = "hello"
var a = 10
/

1.var 只是告诉编译器这个是一个变量,具体是什么类型还是要告诉编译器的

*/
var Scenes,scripts,sounds:String

let swfitone,swifttow:String

swfitone = "Something Just Like This , this is sing"
swifttow = "The is New swift String "
Scenes = "Test switf string practice Unity scenes"
scripts = "This is test unity Scripts Ptactice"
sounds = "I Don,t Understand This Varlable"
/*
这个是一个常量赋值过了,就不能修改了。!
swfitone = "time show paly"
if type Don,t Agree,Don,t JiSuan ,Swift No Support
如果类型不一致,不能直接计算,swfit 不支持自动类型转换
*/
var Myname,speed:NSInteger
speed = 10

//墙砖类型,跟oc是一样的。
var Myprice = Double(speed);

var stringtest = "YinShiShengm"

var StringtestTo:String = "XianShishengM";
//打印与C# 一致
print(Myprice)
//-----The Above Let Vat Statement Over -----

let Error = (404," Net Work Not Found")

//var DataSouer:String

print(Error.0)
let (code,status) = Error

print("code = (code)status = (status)")

//var Mystring:String?="8888"
//print(Mystring)
var Mystring :String? = "9999"
if var testChapao = Mystring{

print("如果有值就赋值,没有值就不赋值");
testChapao = Mystring!;
print(testChapao)

}else{
print("str是nil")//str不赋初始值时

}

/*
1.字符串方法
2.拼接,追加
*/

var stringT = "stringTest1"

var stringT2 = "stringTest2"
//相加
var stringcomporer = stringT+stringtest
//追加C字符
stringT.append(Character("G"))
//追加字符串
stringT.append("Srroy")

print("string == (stringT)")
//追加
var string5 = "(stringT)china"

print("string5 = (string5)")
//强转
var string6 = "100"
var string7 = "300"
//跟OC是一样的
var string8 = Int(string6)

var floatValue = Float(string7)
var string9 = String(floatValue!)

/*
1.在swfit里 所有的变量,用?修饰,并且没有初值,都是nil
2.? opitional 可选值,要么为nil
3.!拆包,如果要拿出变量的可选值,就必须拆包
4.在swfit中,如果声明一个变量,不用?或!修饰的话,他是没有值的,使用时会崩溃的
*/
//这样就定义了一个可选的int型
var Monye:Int? = 100
if Monye == nil {
print("没有值")
}else{
print("有值")
}

//如果要拿出来变量的值就必须拆包。
print(Monye!)
var Sugestring:String? = "88"
print(Sugestring!)
//如果是这样,就一定会有值!可以不用拿直接打印的
var discount:Float! = 3.5
print(discount)

var IntZh = Int(string5)

//Obcjet.isEmpty
if string5.isEmpty {
print("字符串为空")
}else{

print("字符串不为空")

}

if string7 == string5 {
print("字符串相等")
}else{

print("字符串不等")

}

string7.hasPrefix("string")

string7.hasSuffix("str")

//print(JsonData!)

//var jsonstr2 = String(JsonData!, String.Encoding.utf8)
//var jsonStr2 = String(data: JsonData!, String.Encoding.utf8)
//var Jsonsrt2 = String(Data:JsonData!,encoding:NSUTF8StringEncoding)
var Jsonstr = "stringDataholle"
var JsonData:Data? = Jsonstr.data(using: String.Encoding.utf8)
//这个为什么一定要拆包,我擦!
var sss:String! = String.init(data: JsonData!, encoding: String.Encoding.utf8)

var stringjsonself = sss

print(stringjsonself!)

//循环,向下转换,判断,switch
var view:UIView = UIImageView (frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
//as 还真的不知道怎么用,是什么意思
var imageview:UIImageView = view as! UIImageView
//这个是不能够 基本数据类型的
var obj:AnyObject = imageview
//这个才能弄基本数据类型
var objc:Any = "stringName"
print(objc)

//

//数组操作
//var arr1 = ["A","B","C","D",1,2]
//隐士转化基本上不行了,这是其他方法的实例化
var arrint:[Int] = [1,2,3,4]
var arrlintl:Array = [1,2,3,4]
var arrstring:Array = Array()

//arrstring.append("wudi")
//arrstring.append("TL")
arrstring.append("ssss")
arrstring.append("shenwukong")

arrstring.append(contentsOf: ["hehe","wukongz"])

for itmess in arrstring {
print(itmess)
}
//[]其实这个代表一个数组
var arrFloar:[Float] = Float
//,你可以像C#一样的
var Arrfloat:Array = Array()
//初始化方法,都是一样的,这个每个都是结构体了,
var arrstring1:[String] = Array.init(repeating: "wudi", count: 3)

let index:Int = arrstring1.startIndex

let count:Int = arrstring1.count

print("count = (count)")

arrstring1[0] = "Tlzuishai"
//移除全部元素
arrstring1.removeAll()
//判断是否为空
if arrstring.isEmpty {
print("empty")
}else{

print("No empty")

}

if arrstring1 == arrstring {
print("数组相等")
}else{

print("数组不相等")

}

//枚举
enum DeviceType{

case Iphone4
case iphone4s
case iphone5
case iphone5s
case iphone6
case iphone6s
case iphone7
case iphone7pules

}
var type = DeviceType.iphone7

if type == DeviceType.iphone7 {
print("是iphone 7")
}else{

print("不是iphone 7 ")

}

//字典,遍历
var arrdic:[String] = ["china","USA","Japan","ShangHai"]

//遍历时可以获取下标和值
for (indexs,vale2) in arrdic.enumerated() {
print("index==(indexs),values ==(vale2)")
}

//
var stringfor = "i lover MySelf"
//变量字符串
for temo in stringfor.characters {
print("forChai=(temo)")
}

//var dic:[Dictionary] = ["page":"1","money":"100",1:1]

var dic:[Int:Int] = [1:2,3:4]

var dic1:Dictionary = [1:"123",2:"456"]

//实例化,要不前面给他 要不后面给他 实例化
var dic2 = Dictionary()

var dic3:Dictionary = Dictionary.init()

dic3.updateValue("Tl", forKey: 1.0)

print(dic3)

//他没有第一个0元素的
dic3[1] = nil

print(dic3)

dic3.removeValue(forKey: 1.0)

var Dic4:Dictionary = [1:"Tl",2:"baw",3:"wudi"]
//获取每个key,获取字典的每个数值
var keys = Array(Dic4.keys)

for i in keys {
print("K = (i)")
}
var valuse = Array(Dic4.values)

for v in valuse {
print("valus = (v)")
}

for tmp in Dic4 {
print("key = (tmp.0)valus = (tmp.1)")
}
for (keys,valus2) in Dic4 {
print("key = (keys) valus2 = (valus2)")
}
//方法的声明
func test(){

print("这个跟C#一样")

}
test()
//函数名,参数,返回值
func Sum(a:Int,B:Int)->Int{

return a+B

}

var maxnumber = Sum(a: 10, B: 2)

print("maxnumber==(maxnumber)")
//返回多个数据类型
func Nax(a:Int,b:Int,c:Int)->(Int){

return a+b+c

}

func Min (a:Int,B:Int,C:Int)->(Int){

return a-B-C

}
func getMaxandMin(q:Int,W:Int,C:Int)->(Int,Int){

var CurrenMax = q
var CurrentMin = W
CurrenMax = Nax(a: W, b: q, c: C)

CurrentMin = Min(a: q, B: W, C: C)

return (CurrenMax,CurrentMin)

}

var (max1,min1) = getMaxandMin(q: 10, W: 1, C: 1)

print("max1 = (max1)")

print("min1 = (min1)")
//inout 在内部修改传过来的值
func getLnout (y:Int){

}

//下划线的使用,元祖 是一个类型
let point = (x:100,y:100,Z:100)

let (_,y,Z) = point

print("y = (y)")
print("z = (Z)")

var Ts:String? = "ssssss"
if var _ = Ts {
print("来了")
}else{

print("没有")

}

你可能感兴趣的:(Swift 学习笔记(基本类型))