Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 创建具有多个计算函数的简单类()_Swift_Function_Class_Add - Fatal编程技术网

Swift 创建具有多个计算函数的简单类()

Swift 创建具有多个计算函数的简单类(),swift,function,class,add,Swift,Function,Class,Add,尝试通过类中的函数运行简单的计算。我只想添加bill1+bill2,然后打印账单上的总金额。因此(账单1+账单2=总计)。然后打印总金额 当前错误状态-“返回”后的代码将永远不会执行。“现在,我的打印位置是否在错误的位置,或者我是否错误地声明了变量?”?我应该使用vars而不是let吗 为了计算和打印结果,您对我的函数有什么建议 首先:“将永远不会执行“return”之后的代码。” 是的,它不会,在您调用return之后,您将退出函数并返回到调用它的函数,您可能会在XCode中看到一条警告,警

尝试通过类中的函数运行简单的计算。我只想添加bill1+bill2,然后打印账单上的总金额。因此(账单1+账单2=总计)。然后打印总金额

当前错误状态-“返回”后的代码将永远不会执行。“现在,我的打印位置是否在错误的位置,或者我是否错误地声明了变量?”?我应该使用vars而不是let吗

为了计算和打印结果,您对我的函数有什么建议


首先:“将永远不会执行“return”之后的代码。”

是的,它不会,在您调用return之后,您将退出函数并返回到调用它的函数,您可能会在XCode中看到一条警告,警告您不要告诉您这一点

Second:“我应该使用vars而不是let吗?”

如果值发生变化,则必须使用var,否则应使用let

我可以在您的代码中看到一些问题:

class BillsCalculator
{
    //use _ in the beginning of the name for class variables
    //eg. _nameOfBill instead nameOfBill1
    //It is not wrong use nameOfBill1 is just not recommended 
    //if nameOfBill1 change use var
    let nameOfBill1: String = "Medical"
    //Why is this declare twice
    let nameOfBill1: String = "Hulu" 
    //Those values look like change should be var
    var monthlyBillAmount1: Double = 34.25
    var monthlyBillAmount2: Double = 7.99
    var calculateTotalsPerMonth: Double = 0.0
    func calculateTotalsPerMonth(monthlyBillAmount: Double, monthlyBillAmount2: Double) -> Double
    {
        totalBillsPerMonth = add(monthlyBillAmount1 + monthlyBillAmount2)
        //print before return
        println("You spend \(totalBillsPerMonth)")
        return totalBillsPerMonth(monthlyBillAmount1 + monthlyBillAmount2)

    }
}

在这里,您应该打印总账单的价值或返回该价值。因为您只想打印账单总额,所以我建议您只打印,不返回任何内容。您可以参考下面的代码

class BillsCalculator
{
    let nameOfBill1: String = "Medical"
    let nameOfBill1: String = "Hulu"
    let monthlyBillAmount1: Double = 34.25
    let monthlyBillAmount2: Double = 7.99
    let calculateTotalsPerMonth: Double = 0.0

   //calculateTotalPerMonth ( = monthlyBillAmount_1 + monthlyBillAmount_2 + 3)

    func calculateTotalsPerMonth(monthlyBillAmount: Double, monthlyBillAmount2: Double) -> Double
    {
        calculateTotalsPerMonth= add(monthlyBillAmount1 + monthlyBillAmount2)
             println("You spend : "+totalBillsPerMonth);

    }
}

代码中的一个小错误

    let nameOfBill1: String = "Medical"
    let nameOfBill1: String = "Hulu"
这两个变量具有相同的名称,也许其中一个应该是:

    let nameOfBill2: String = "Hulu"
并且yes return始终是函数中的最后一行,因此return之后的任何代码都不会执行。如果您只想得到两张账单的总数,您可以简单地执行以下操作:

    func calculateTotalsPerMonth(monthlyBillAmount: Double, monthlyBillAmount2: Double) -> Double {
         //println("You spend \(totalBillsPerMonth)")
         return monthlyBillAmount1 + monthlyBillAmount2
     }
并使用账单变量调用此函数,如:

    let bill1 = 34.25
    let bill2 = 7.99
    let totalBill = calculateTotalsPerMonth(bill1, bill2)
    println("You spent \(totalBill)")
Swift是一种非常智能的语言,它是类型安全的。如果需要,您可以删除该类型,更像是一种个人编程风格

    let bill1: Double = 34.25
    let bill1 = 34.25

它们都将是类型“Double”

,正如其他人所说,您需要将
println
语句放在
return
之前,因为returns结束了方法的执行;因此,
println
将永远不会运行

但是,我建议对您当前的方法进行一些更改:

// A bill is an object - why not encapsulate it in a struct.
struct Bill {
    let name: String
    let amount: Double
}

// Using structs is generally preferred, unless you need inheritance and/or
// references to your BillsCalculator objects.
struct BillsCalculator {
    let bill1: Bill
    let bill2: Bill

    // Using a read-only computed property means you don't need to set 
    // the total to have an initial value of zero.
    var totalBilled: Double {
        return bill1.amount + bill2.amount
    }
}

// Since you're probably going to want to reuse BillsCalculator, 
// don't have each bill set already. Instead, use BillsCalculator's 
// initialiser and pass in bills.
let bill1 = Bill(name: "Medical", amount: 34.25)
let bill2 = Bill(name: "Hulu",    amount: 7.99)
let cal = BillsCalculator(bill1: bill1, bill2: bill2)

print("You've spend \(cal.totalBilled) this month")

谢谢你调查这件事。我的评论中有_bill1和_bill2,但不确定为什么我没有捕捉到那些匹配的变量。我可能也会将_账单更改为var,因为它可能会更改。我很高兴我能提供帮助,让我们知道它现在是否工作正常,如果工作正常,请不要忘记将其中一个答案标记为正确:)祝你好运,完成你的应用程序!你的结构建议奏效了。我在xcode操场上做这件事,你们的建议给了我最近的项目一些信心。结构不在我最近看的林达教程中。再次感谢。您对该功能的评论无效。错误:使用未解析的标识符“add”&另一个错误:无论我在println()中更改了什么,都会不断更改。任何我最喜欢你的格式的建议,也是最有意义的,但是缺少了一些东西。只需替换calculateTotalsPerMonth=add(monthlyBillAmount1+monthlyBillAmount2);使用calculateTotalsPerMonth=(monthlyBillAmount1+monthlyBillAmount2);
// A bill is an object - why not encapsulate it in a struct.
struct Bill {
    let name: String
    let amount: Double
}

// Using structs is generally preferred, unless you need inheritance and/or
// references to your BillsCalculator objects.
struct BillsCalculator {
    let bill1: Bill
    let bill2: Bill

    // Using a read-only computed property means you don't need to set 
    // the total to have an initial value of zero.
    var totalBilled: Double {
        return bill1.amount + bill2.amount
    }
}

// Since you're probably going to want to reuse BillsCalculator, 
// don't have each bill set already. Instead, use BillsCalculator's 
// initialiser and pass in bills.
let bill1 = Bill(name: "Medical", amount: 34.25)
let bill2 = Bill(name: "Hulu",    amount: 7.99)
let cal = BillsCalculator(bill1: bill1, bill2: bill2)

print("You've spend \(cal.totalBilled) this month")