首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift NSNumberFormatter

Swift NSNumberFormatter
EN

Stack Overflow用户
提问于 2015-09-20 04:38:01
回答 1查看 579关注 0票数 0

我正在快速阅读我的书,边走边建。我在使用这个NSNumberFormatter类时遇到了问题。在我的语句之后,我一直收到错误:“期望的声明”。

代码语言:javascript
复制
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle

我已经重写了我的代码多次,我把每一个字母都与我的书进行了比较,我不知道我哪里错了

当我尝试使用以下命令运行函数时:

代码语言:javascript
复制
account1.deposit(depositAmount)

我再次收到错误消息"Expected“

下面是我的代码:

代码语言:javascript
复制
    //: 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")
    }

提前谢谢你!

EN

回答 1

Stack Overflow用户

发布于 2015-09-20 04:41:35

问题很可能出在formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle的位置上。您不能从类级别运行代码。将其移动到init。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32672971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档