Day1 (let and var)/(String,Int,Float,Double)/(Tuples,contains: array,dictionary)

本文通过几个简单的Swift代码示例介绍了变量定义、类型转换、元组等基本概念,并展示了如何处理字符串与整型之间的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

let secondSystem:Int = 0b10001

let eightSystem:Int  = 0o21

let tenSystem:Int    = 17

let sixthSystem:Int  = 0x11

// summary: 1.u need insert a "Enter" behind equal symbol 

 

let num_a:Float = 1

let num_b:Int = 1

 

let a:Int = 3

let b:Double = 0.1415926

let pi:Double = Double(a) + b

// summary: 2.it need change two number's type in the same ,in order to getting the result

 

let bignum_a:Float = 10/2.5

let bignum_a2:Int = 10/3

let bignum_b:Float = Float(Int(1.2))*10.1

let bignum_c:Int = Int(3.99)

// summary: 3.it need change two number's type in the same ,in order to getting the result

//          4.forcing number type need add type symbol in the front of number

 

let jy = "爱下厨"

let address = ".Jianggan,Hangzhou City,ZheJiang Province,China"

jy + address

 

let params = (isLoginSuccess:true,name:"tommy",age:25)

let (isLoginSuccess,name,age) = params

let isLginSuccess = params.0

let whats = params.name

let whatsage = params.2

 

let (isTrue,_,_) = params

if (isTrue)

{

    print("login success!")

}

 

let personArray = ("a","b","c","d")

 

let aPerson = personArray.0

let bPerson = personArray.1

// summary: 5. Tuples

 

var value_a:Int

value_a = 1

 

var imOptionalVariable:Int?

// summary: it's equal to nil when the optionalVariable is empty

 

imOptionalVariable = 56

 

let name = "Tommy"

var age = Int(name)

 

if age != nil

{

    print("it's not empty")

}

else {

    print("it's empty!")

}

 

let userage = "25"

var currentAge = Int(userage)

let currentnewAge = Int(userage)

// summary: String toInt use method : Int(string)

 

if currentAge != nil

{

    print("age: \(currentAge)")

    print("his age is " + String(currentAge!))

}

 

if userage .isEmpty

{

    print("age: \(userage)")

}else {

    print("age :\(userage)")

}

 

let stringA:String? = "optional_String"

let string2 = "_/stringA"

var stringB = stringA! + string2

stringA

stringA!

转载于:https://www.cnblogs.com/tony0571/p/5346442.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值