7.Xcode for Swift

Xcode

IDE(Integrated Development Environment)集成開發環境swift

ViewController.swiftide

import Cocoa

class ViewController: NSViewController {
	//"!"爲隱式封拆的可選類型,至關於告訴編譯器,在該變量爲nil時毫不會訪問它
	@IBOutlet weak var loanAmountField :NSTextField!
	@IBOutlet weak var interestRateField : NSTextField!
	@IBOutlet weak var yearsField : NSTextField!
	@IBOutlet weak var resultsField : NSTextField!
	var simpleInterestCalculator : SimpInterest = SimpInterest()
	
	override func viewDidLoad() {
		super.viewDidLoad()

		// Do any additional setup after loading the view.
	}

	override var representedObject: Any? {
		didSet {
		// Update the view, if already loaded.
		}
	}
	[@IBAction](https://my.oschina.net/u/866341) func buttonClicked(sender : NSButton){
		var result : Double
		result = simpleInterestCalculator.calculate(loanAmount: loanAmountField.doubleValue, interestRate: interestRateField.doubleValue , years: yearsField.integerValue)
		self.resultsField.stringValue = result.description//description讓類可以返回其數據的String表示
		
	}


}

SimpInterest.swift.net

import Foundation


class SimpInterest{
	func calculate(loanAmount : Double ,interestRate :Double,years : Int) -> Double{
		let interestRate1 = loanAmount / 100.0
		let interest = Double(years) * interestRate1 * interestRate//單利計算
		return interestRate + interest
	
	}

}

輸入圖片說明

相關文章
相關標籤/搜索