Swift實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
用Swift寫一個(gè)簡(jiǎn)單計(jì)算器的Demo,供大家參考,具體內(nèi)容如下
實(shí)驗(yàn)環(huán)境:
Xcode v6.4 & OS X Yosemite 10.10
功能描述:
1、實(shí)現(xiàn)加減乘除+根號(hào)(結(jié)果display為Double型)
2、邊界適應(yīng):各元素之間的距離固定,且適應(yīng)手機(jī)旋轉(zhuǎn)(Roate)
(學(xué)習(xí)過程,根據(jù)Stanford的Swift課程而寫的程序)

代碼實(shí)現(xiàn):
//
// ?ViewController.swift
// ?Calculator
//
// ?Created by VincentYau on 4/7/16.
// ?Copyright (c) 2016 VincentYau. All rights reserved.
//
import UIKit
class ViewController: UIViewController
{
? ? @IBOutlet weak var display: UILabel!
? ? var userIsInTheMiddleOfTypingANumber:Bool = false
? ? //用戶是否已經(jīng)輸入數(shù)字,由于Swift的變量必須負(fù)初始值,所以設(shè)為false
? ? @IBAction func appendDigit(sender: UIButton){
? ? ? ? let digit = sender.currentTitle!//直接獲取Button的數(shù)字
? ? ? ? //若已輸入過數(shù)字,則直接往display中添加數(shù)字,否則直接現(xiàn)實(shí)新點(diǎn)擊數(shù)字,去除原始0的操作
? ? ? ? if userIsInTheMiddleOfTypingANumber{
? ? ? ? ? ? display.text = display.text! + digit
? ? ? ? }else{
? ? ? ? ? ? display.text = digit
? ? ? ? ? ? userIsInTheMiddleOfTypingANumber = true
? ? ? ? }
? ? }
? ? //對(duì)數(shù)字進(jìn)行運(yùn)算
? ? @IBAction func operate(sender: UIButton) {
? ? ? ? let operation = sender.currentTitle!
? ? ? ? if userIsInTheMiddleOfTypingANumber{
? ? ? ? ? ? enter()
? ? ? ? }
? ? ? ? switch operation{
? ? ? ? /*swift算法極為簡(jiǎn)潔,當(dāng)調(diào)用方法performOperation時(shí),其自動(dòng)對(duì)比方法的參數(shù),而無需在
? ? ? ? ?*調(diào)用方法時(shí)寫明參數(shù)類型,例如,這里的參數(shù)$0 與 $1并沒有指明類型,而Swift會(huì)直接將其適應(yīng)為
? ? ? ? ?*方法performOpetation中的Double型
? ? ? ? */
? ? ? ? case "×": performOperation { $0 * $1 }
? ? ? ? case "÷": performOperation { $1 / $0 }
? ? ? ? case "+": performOperation { $0 + $1 }
? ? ? ? case "?": performOperation { $1 - $0 }
? ? ? ? case "√": performOperation { sqrt($0) }
? ? ? ? default: break
? ? ? ? }
? ? }
? ? //兩個(gè)參數(shù)進(jìn)行運(yùn)算的方法
? ? func performOperation(operation: (Double,Double) -> Double){
? ? ? ? if operandStack.count >= 2 {
? ? ? ? ? ? displayValue = operation(operandStack.removeLast(),operandStack.removeLast())
? ? ? ? ? ? enter()
? ? ? ? }
? ? }
? ? //一個(gè)參數(shù)進(jìn)行運(yùn)算的方法,Swift支持方法的重載,但Obj-C不允許,這里繼承了Obj-C的
? ? //類UIViewColler,不能重載方法performOperation,故將其變?yōu)镻rivate方法
? ? private func performOperation(operation: Double -> Double){
? ? ? ? if operandStack.count >= 1 {
? ? ? ? ? ? displayValue = operation(operandStack.removeLast())
? ? ? ? ? ? enter()
? ? ? ? }
? ? }
? ? var operandStack = Array<Double>() ?
? ? //若用戶點(diǎn)擊enter,則將相應(yīng)數(shù)字添加至數(shù)組Array中 ?
? ? @IBAction func enter() {
? ? ? ? userIsInTheMiddleOfTypingANumber = false
? ? ? ? operandStack.append(displayValue)
? ? ? ? println("operandStack = \(operandStack)")
? ? }
? ? var displayValue: Double {
? ? ? ? get{
? ? ? ? ? ? return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
? ? ? ? }
? ? ? ? set{
? ? ? ? ? ? display.text = "\(newValue)"
? ? ? ? ? ? userIsInTheMiddleOfTypingANumber = false
? ? ? ? }
? ? }
}注意:
這里容易忽略的是,各元素之間的距離還有元素與邊界的距離,設(shè)置好后如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
EvenLoop模型在iOS的RunLoop應(yīng)用示例
這篇文章主要為大家介紹了EvenLoop模型在iOS的RunLoop應(yīng)用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
舉例講解Swift編程中switch...case語(yǔ)句的用法
這篇文章主要介紹了Swift編程中switch...case語(yǔ)句的用法,其中fallthrough關(guān)鍵字在switch語(yǔ)句中的使用是重點(diǎn),需要的朋友可以參考下2016-04-04
Swift自動(dòng)調(diào)整視圖布局AutoLayout和AutoresizingMask功能詳解
這篇文章主要為大家介紹了Swift自動(dòng)調(diào)整視圖布局AutoLayout和AutoresizingMask功能及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Swift?中?Opaque?Types學(xué)習(xí)指南
這篇文章主要為大家介紹了Swift?中?Opaque?Types學(xué)習(xí)指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Swift實(shí)現(xiàn)表格視圖單元格單選(2)
這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)表格視圖單元格單選的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01

