gpt4 book ai didi

swift NSNumberFormatter

转载 作者:行者123 更新时间:2023-11-28 13:06:18 28 4
gpt4 key购买 nike

<分区>

我正在阅读我的 swift 书并边做边建。我在使用这个 NSNumberFormatter 类时遇到了问题。我不断收到错误: “预期声明”在我声明之后

formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle

我已经多次重写我的代码,我已经将每封信与我的书进行了比较,但我不明白我哪里出了问题

当我尝试运行函数时:

account1.deposit(depositAmount)

我再次收到“Expected Declaration”的错误信息

这是我的代码:

    //: Playground - noun: a place where people can play

import Cocoa
import Foundation

public class Account
{
public var name: String = ""
public var balance: Double = 0.0


public init(name: String, balance: Double)
{
self.name = name
if balance > 0.0
{
self.balance = balance
}
}

public func deposit(amount: Double)
{
if amount > 0.0
{
balance = balance + amount
}
}

public func withdraw(amount: Double)
{
if balance - amount >= 0.0
{
balance = balance - amount
}
}



let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle


func formatAccountString(account: Account) -> String {
return account.name + "'s balance: " + formatter.stringFromNumber(account.balance)!
}




let account1 = Account(name: "Jane Green", balance: 50.00)
let account2 = Account(name: "John Blue", balance: -7.53)

//println(formatAccountString(account1))

var depositAmount = 25.53
account1.deposit(depositAmount)
account2.deposit(depositAmount)

println("depositing " + formatter.StringFromNumber(depositAmount) + " into account1\n")
println("depositing " + formatter.StringFromNumber(depositAmount) + " into account1\n")
}

提前致谢!

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com