前段時間仿iPhone計算機寫的Dome如圖(已經(jīng)完成Auto Layout)麦备,現(xiàn)在來記錄一些細節(jié)上的優(yōu)化铃肯。
先感謝Prayer等團隊翻譯的The Swift Programming Language( 中文版): http://wiki.jikexueyuan.com/project/swift/chapter2/03_Strings_and_Characters.html在該文檔中找到了Dome中使用String的所有用法涎显。
項目dome已經(jīng)上傳GitHub:
***https://github.com/BigTortoise/CalculatorLikeApple ***
精度控制
在Dome中設(shè)置成保留10位有效數(shù)字暗膜,并使用四舍五入的保留有效數(shù)位彰导。
//聲明一個常量count記錄運算結(jié)果字符的長度
let count = display.text!.characters.count
if count >= 11 {
//截取前11位字符
let OneToTen = (display.text! as NSString).substringToIndex(11)
//截取前9位字符
let OneToN = (display.text! as NSString).substringToIndex(9)
//提取前11位字符中第11位字符所踊,也就是最后一位
let end11 = (OneToTen as NSString).substringFromIndex(10)
//提取前10位字符中第10和11位字符
var end10 = (OneToTen as NSString).substringFromIndex(9)
//如果第11位數(shù)字大于5泌枪,那么就要向前進位,也就是第10位+1
if Int(end11) >= 5 {
end10 = String(Int(end10)! + 1)
display.text = OneToN + end10
}
}
上限控制
仿造iPhone的效果秕岛,我設(shè)置用戶能輸入的位數(shù)上限位10個有效數(shù)位碌燕,運算后若數(shù)位超過10位有效數(shù)字,則用科學(xué)計數(shù)法表示继薛,如圖進行運算時修壕,1234567890*10,得出的結(jié)果為:1.23456789e11
遏考。
// 創(chuàng)建operandStack數(shù)組慈鸠,記錄運算的兩個數(shù)子
var operandStack = Array<Double>()
// 記錄第幾次輸入數(shù)字
var userIsInTheMiddleOfTypingANumber : Bool = false
//運算方法,結(jié)果再次存入operandStack數(shù)組
func performOperation(operation : (Double,Double) -> Double ) {
if operandStack.count >= 2{
displayValue = operation(operandStack.removeLast(),operandStack.removeLast())
userIsInTheMiddleOfTypingANumber = false
operandStack.append(displayValue)
}
}
// = 按鈕的進行運算灌具,調(diào)用performOperation的方法
@IBAction func equals(sender: UIButton) {
if userIsInTheMiddleOfTypingANumber {
userIsInTheMiddleOfTypingANumber = false
operandStack.append(displayValue)
}
let counts = display.text!.characters.count
if counts >= 10 {
//display.text = (display.text! as NSString).substringToIndex(10)
if userIsInTheMiddleOfTypingANumber {
userIsInTheMiddleOfTypingANumber = false
operandStack.append(displayValue)
}
switch remberTheMathematicalSign {
case "×" : performOperation {$0 * $1}
case "÷" : performOperation {$1 / $0}
case "+" : performOperation {$0 + $1}
case "?" : performOperation {$1 - $0}
default:break
}
TheTimeUserClickButton = ++TheTimeUserClickButton
}else{
switch remberTheMathematicalSign {
case "×" : performOperation {$0 * $1}
case "÷" : performOperation {$1 / $0}
case "+" : performOperation {$0 + $1}
case "?" : performOperation {$1 - $0}
default:break
}
TheTimeUserClickButton = ++TheTimeUserClickButton
}
//聲明一個常量count記錄運算結(jié)果字符的長度
let count1 = display.text!.characters.count
var theNumberOfZero = 1
//計算出位數(shù)
for var i = 6; i < count1; ++i {
theNumberOfZero *= 10
}
//截取前八位字符
let OneToTen = (display.text! as NSString).substringToIndex(8)
//進行運算
let x = Double(OneToTen)! / Double(theNumberOfZero)
//再次進行截取
let Y = (String(x) as NSString).substringToIndex(8)
//字符串拼接
display.text = String(Y) + "e\(count1-2)"
Dome中String的一些用法
- 聲明一個常量count記錄運算結(jié)果字符的長度
let count1 = display.text!.characters.count
- 截取前11位字符
let OneToTen = (display.text! as NSString).substringToIndex(11)
- 提取11位字符串中第11位字符青团,也就是最后一位
let end11 = (OneToTen as NSString).substringFromIndex(10)
- 字符串拼接
display.text = String(Y) + "e\(count1-2)"
其他String的一些用法
- 字符串插值
let multiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// message is "3 times 2.5 is 7.5"
- 計算字符數(shù)量
let unusualMenagerie = "Koala ??, Snail ??, Penguin ??, Dromedary ??"
print("unusualMenagerie has \(unusualMenagerie.characters.count) characters")
// 打印輸出 "unusualMenagerie has 40 characters"
- 插入和刪除
- 調(diào)用insert(_:atIndex:)方法可以在一個字符串的指定索引插入一個字符譬巫。
var welcome = "hello"
welcome.insert("!", atIndex: welcome.endIndex)
// welcome now 現(xiàn)在等于 "hello!"
- 調(diào)用insertContentsOf(_:at:)方法可以在一個字符串的指定索引插入一個字符串。
welcome.insertContentsOf(" there".characters, at: welcome.endIndex.predecessor())
// welcome 現(xiàn)在等于 "hello there!"
- 調(diào)用removeAtIndex(_:)方法可以在一個字符串的指定索引刪除一個字符督笆。
welcome.removeAtIndex(welcome.endIndex.predecessor())
// welcome 現(xiàn)在等于 "hello there"
- 調(diào)用removeRange(_:)方法可以在一個字符串的指定索引刪除一個子字符串芦昔。
let range = welcome.endIndex.advancedBy(-6)..<welcome.endIndex
welcome.removeRange(range)
// welcome 現(xiàn)在等于 "hello"