Swift 编程注意事项
来源:互联网 发布:云计算hadoop实战视频 编辑:程序博客网 时间:2024/05/14 22:57
1: 值永远不会被隐式转换为其他类型。如果你需要把一个值转换成其他类型,请显式转换。
let label = "the width is"let width = 95let widthLabel = label + String(width)print(widthLabel)
2:有一种更简单的把值转换成字符串的方法:把值写到括号中,并且在括号之前写一个反斜杠
let str1 = 90.0let str2 = 90let str = str1 + Double(str2)let str3 = "niki 身高:"print("\(str3) \(str)")
3:类型推断
如果表达式中同时出现了整数和浮点数,会被推断为 Double 类型:
let meaningOfLife = 42//被推测为 Int 类型let pi = 3.14159 //被推测为 Double 类型let anotherPi = 3 + 0.14159// anotherPi 会被推测为 Double 类型
浮点数到整数的反向转换同样行,整数类型可以用 Double 或者 Float 类型来初始化:
let integerPi = Int(pi)// integerPi 等于 3,所以被推测为 Int 类型
注意:当用这种方式来初始化一个新的整数值时,浮点值会被截断。也就是说 4.75 会变成 4 , -3.9 会变成 -3 。
0 0
- Swift 编程注意事项
- swift入门- UITableview注意事项
- swift 常量使用注意事项
- swift 函数使用注意事项
- 编程注意事项
- 编程注意事项
- 编程注意事项
- 编程注意事项
- 编程注意事项
- 编程注意事项
- 编程注意事项
- 编程注意事项
- 编程注意事项
- 编程注意事项
- Swift和Objective-C注意事项
- swift中tableview的使用和注意事项
- Swift中使用protocol的注意事项
- Swift学习笔记三:运算符注意事项
- Java简单介绍
- 安装zabbix proxy
- iOS给UIButton添加一个新的属性
- 第一步
- mysql读写分离 amoeba
- Swift 编程注意事项
- View的事件拦截机制
- 面试笔试杂项积累-leetcode 6-10
- linux中shell逻辑表达式if else以及大于、小于、等于
- 有序数列中插入一个数,使之仍有序
- 日积月累--android基础知识日记
- 2016年1月25日 孩子 你可要健健康康!!
- IOS开发 使用XIB自定义一个UIView
- 碰到一个让我的电脑跑到歇菜的非线性方程组