1. 获取 iOS 文本框输入的值
@IBOutlet weak var text: UITextField!
var text = text.text!
print(text)
注意,text.text 为 Optional 类型,需要加上 ! 来拆包。
2. 将字符串转化为 utf-8 编码
var nn = text.utf8CString
nn.remove(at: nn.count-1)
3. 将字符串转换为字符数组
var wm2 = wm1.map {String($0)}
print(wm2)
4. 取整
ceil(x)返回不小于x的最小整数值(然后转换为double型)。
floor(x)返回不大于x的最大整数值。
round(x)返回x的四舍五入整数值。
5. iOS 弹窗
let alertView = UIAlertController.init(title: "提示", message: "信息", preferredStyle: .alert)
let alert = UIAlertAction.init(title: "确定", style: .destructive) { (UIAlertAction) in
print("确定按钮点击")
}
let cancleAlert = UIAlertAction.init(title: "取消", style: .cancel) { (UIAlertAction) in
print("点击取消按钮")
}
alertView.addAction(cancleAlert)
alertView.addAction(alert);
self.present(alertView, animated: true, completion: nil)
6. Int 转字符串
let x : Int = 45
var stringValue = "\(x)"
print(stringValue)
7. 字符串截取
let string = "12345678"
let indexN = string.index(string.startIndex, offsetBy: 1)
let indexM = string.index(string.startIndex, offsetBy: 3)
let substr = string[indexN..<indexM]
let substr_1 = string[indexN...indexM]
8. 将二进制字符串转换为ASCII值
let binary = "11001"
if let number = Int(binary, radix: 2) {
print(number)
}
|