##8.1数字格式设置(storyboard) ##8.1.2可选类型拆封
import Cocoaextension Double{ var dollars : String{ let formatter : NumberFormatter = NumberFormatter()//NSNumberFormatter弃用 var result : String?//将result的类型声明为可选类型 formatter.numberStyle = .currency result = formatter.string(from: NSNumber(value:self))// .string(from: NSNumber)返回为可选类型,因为如果转换失败会返回nil if (result == nil){ return "FORMAT FAILURE!" } return result!//拆封可选变量,拆封指的是将变量转换为值不能为nil的变量 }}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() var compoundInterestCalculator : CompoundInterest = CompoundInterest() override func viewDidLoad() { super.viewDidLoad() } override var representedObject: Any? { didSet { } } [@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表示 self.resultsField.stringValue = result.dollars } @IBAction func compoundbuttonClicked(_ sender: NSButton) { var result : Double result = compoundInterestCalculator.calculate(loanAmount: loanAmountField.doubleValue, interestRate: interestRateField.doubleValue , years: yearsField.integerValue) // self.resultsField.stringValue = result.description//description让类能够返回其数据的String表示 self.resultsField.stringValue = result.dollars }}
8.2单元测试 if Use of undeclared type 'SimpInterest' take class Target Membership choose
import XCTestclass MACSwiftTeststwo: XCTestCase { var mySimpInterestCalculator : SimpInterest = SimpInterest() var myCompoundInterestCalculator : CompoundInterest = CompoundInterest() override func setUp() { super.setUp() // Put setup code here. This method is called before the invocation of each test method in the class. //在这里插入设置代码,调用这个类中的每个测试方法前,都将调用这个方法 } override func tearDown() { // Put teardown code here. This method is called after the invocation of each test method in the class. //在这里插入清理代码,调用这个类中的每个测试方法后,都将调用这个方法 super.tearDown() } //功能测试用例 func testSimpleInterest(){ var result : Double result = mySimpInterestCalculator.calculate(loanAmount: 25_000, interestRate: 0.08, years: 10) XCTAssertEqualWithAccuracy(result,45000,accuracy:0.1 ,"Unexpected result \(result)")//1.要评估的变量 2.该变量应包含的值 3准确因子 4.未通过测试时显示的String //传入的变量和预期结果相差不超过0.1 } func testCompoundInterest(){ var result : Double result = myCompoundInterestCalculator.calculate(loanAmount: 25_000, interestRate: 0.08, years: 10) XCTAssertEqualWithAccuracy(result, 25002+0.1, accuracy: 0.1,"Unexpected result \(result)") } //示例测试方法// func testExample() {// // This is an example of a functional test case.// // Use XCTAssert and related functions to verify your tests produce the correct results.// }// // func testPerformanceExample() {// // This is an example of a performance test case.// self.measure {// // Put the code you want to measure the time of here.// }// } }