我编写了如下所示的代码。如何打印A4变量? 当我这样尝试时,我得到一个错误?
错误:__ lldb_expr_1.Paper
// Paper Factory
// All sizes are mm type
import UIKit
class Paper {
var weight: Double = 0.0
var sizeHeight: Double = 0.0
var sizeWidth: Double = 0.0
init(weight: Double, sizeHeight: Double, sizeWidth: Double){
self.sizeHeight = sizeHeight
self.sizeWidth = sizeWidth
self.weight = weight
}
func paperPrice(weight: Double, sizeHeight:Double, sizeWidth:Double){
var price = (sizeHeight * sizeWidth) * weight / 1000
}
}
var A4 = Paper(weight: 3, sizeHeight: 210, sizeWidth: 297)
println(A4)
答案 0 :(得分:0)
试试这个
NSLog("\(A4)")
它工作得很好......
答案 1 :(得分:0)
在错误消息中,lldb
指的是LLDB,即Xcode调试器的命令行提示符。打印这些值的正确方法是将NSLog
与format specifiers一起使用。例如:
NSLog(@"Value of property : %d", A4.weight)
此外,查看上面的示例代码,price
无法在课程级别访问,因为它是在func paperPrice
范围内定义的。您可能希望定义与price
等类似的weight
,并使用A4.price
进行访问。
答案 2 :(得分:0)
println
可以很好地使您的班级符合Printable
协议。以这种方式实现你的课程。别忘了添加description
属性。
class Paper : Printable{
var weight: Double = 0.0
var sizeHeight: Double = 0.0
var sizeWidth: Double = 0.0
var description: String {
return "Weight: \(weight) sizeHeight: \(sizeHeight) sizeHeight: \(sizeHeight): \(sizeHeight: \(sizeHeight))"
}
}
答案 3 :(得分:0)
您的paperPrice功能没有做任何事情。它将值设置为函数中定义的变量。函数完成后,变量就超出范围。您需要将变量返回到Class之外的内容才能使用它。试试这个:
// Paper Factory
// All sizes are mm type
import UIKit
class Paper {
var weight: Double = 0.0
var sizeHeight: Double = 0.0
var sizeWidth: Double = 0.0
init(weight: Double, sizeHeight: Double, sizeWidth: Double){
self.sizeHeight = sizeHeight
self.sizeWidth = sizeWidth
self.weight = weight
}
func paperPrice() -> Double {
var weight = self.weight
var sizeHeight = self.sizeHeight
var sizeWidth = self.sizeWidth
var price: Double = (sizeHeight * sizeWidth) * weight / 1000
return price
}
}
var A4paper = Paper(weight: 3, sizeHeight: 210, sizeWidth: 297)
var price = A4paper.paperPrice()
println(price)
答案 4 :(得分:0)
确保计算paperPrice的函数返回一个可以打印的值。
<proxies>
<proxy>
<active>true</active>
<protocol>https</protocol>
<host>localhost</host>
<port>3132</port>
</proxy>
</proxies>