xcode - 初始化器 'init(_:)' 要求 'Decimal' 符合 'BinaryInteger'

标签 xcode swiftui

我正在尝试制作一个复利计算器应用程序,当我返回复利公式时出现此错误 -> Initializer 'init(_:)' 要求 'Decimal' 符合 'BinaryInteger'

执行复利公式时出现错误:
返回余额 * pow((1 + rate/100), Int(mounths))

struct CompoundView: View {
@State var accountBalance: Decimal?
@State var percentagePerMounth: Decimal?
@State var numberOfMounths: Decimal?

@State var totalCompoundInterest = " "

var total: Decimal {
    guard let balance = self.accountBalance, let rate = self.percentagePerMounth, let mounths = self.numberOfMounths else {
        return 0
    }
    return balance * pow((1 + rate / 100), Int(mounths))
}

// Currency Formater
static var currencyFormatter: NumberFormatter {
    let nf = NumberFormatter()
    nf.numberStyle = .currency
    nf.isLenient = true
    return nf
}

// Percent Formatter
static var percentFormatter: NumberFormatter {
    let nf = NumberFormatter()
    nf.numberStyle = .percent
    // preserve input as-is, otherwise 10 becomes 0.1, which makes
    // sense but is less intuitive for input
    nf.multiplier = 1
    nf.isLenient = true
    return nf
}

// Months Formatter
static var monthsFormatter: NumberFormatter {
    let nf = NumberFormatter()
    nf.numberStyle = .decimal
    nf.isLenient = true
    return nf
}

// Compound Interest View
var body: some View {

    VStack {
        HStack {
            Text("Start Balance")
                .font(.subheadline)
                .padding(.vertical)
            Spacer()
            DecimalField("$0.00", value: $accountBalance, formatter: Self.currencyFormatter)
                .keyboardType(.decimalPad)
                .font(.largeTitle)
                .multilineTextAlignment(.trailing)
        }
        .foregroundColor(.white)
        Divider().background(Color.white)

        HStack {
            Text("Percentage per month")
                .font(.subheadline)
                .padding(.vertical)
            Spacer()
            DecimalField("0%", value: $percentagePerMounth, formatter: Self.percentFormatter)
                .keyboardType(.decimalPad)
                .font(.largeTitle)
                .multilineTextAlignment(.trailing)
        }
        .foregroundColor(.white)
        Divider().background(Color.white)

        HStack {
            Text("Number of months")
                .font(.subheadline)
                .padding(.vertical)
            Spacer()
            DecimalField("0", value: $numberOfMounths, formatter: Self.monthsFormatter)
                .keyboardType(.decimalPad)
                .font(.largeTitle)
                .multilineTextAlignment(.trailing)
        }
        .foregroundColor(.white)
        Divider().background(Color.white)

        // Calculate Button
        Button(action: {
            hideKeyboard()
            self.totalCompoundInterest = Self.currencyFormatter.string(for: self.total)!

        }) {
            Text("Calculate")
                .font(.body)
                .foregroundColor(Color.white)
        }

        Text(self.totalCompoundInterest)
            .foregroundColor(.white)
            .font(.largeTitle)
            .padding()

        Spacer()
    }
} 

}

最佳答案

您需要将 mounths 正确转换为 Int。像这样:

Int(truncating: mounths as NSNumber))

所以你在 init 中的最后一行看起来像这样:
return balance * pow((1 + rate / 100), Int(truncating: mounths as NSNumber))

关于xcode - 初始化器 'init(_:)' 要求 'Decimal' 符合 'BinaryInteger',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61090503/

相关文章:

swift - SwiftUI 状态初始值设定项之间的区别

ios - NEON程序集无法在Xcode 4.3.2中为iOS构建

xcode - 如何让 Jenkins 推送到 GitHub 但不触发推送构建

xcode - 无法在 Xcode 6.1 中使用类型为 '==' 的参数列表调用 '($T2, OSStatus)'

iphone - 我已经注册了 App ID,但它仍然显示您没有适用于 iOS 应用程序的合格 Bundle ID。在这里注册一个

SwiftUI:从 AppDelegate 访问 @EnvironmentObject

swift - 为 SwiftUI 应用程序设计类的正确架构是什么

swift - 通过拖动手势更改偏移时 View 不稳定

ios - SwiftUI:对同一变量使用不同的属性包装器

xcode - CocoaPods:多个命令产生