Ios 无法在swift中打印变量

Ios 无法在swift中打印变量,ios,class,swift,variables,println,Ios,Class,Swift,Variables,Println,我编写了如下所示的代码。如何打印A4变量? 当我这样尝试时,我会出错 错误:\ lldb\u expr\u 1.0 // 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,

我编写了如下所示的代码。如何打印A4变量? 当我这样尝试时,我会出错

错误:\ lldb\u expr\u 1.0

// 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)
像这样试试

 NSLog("\(A4)")

它工作正常…

在错误消息中,
lldb
指的是lldb,即Xcode调试器的命令行提示符。打印这些值的正确方法是使用
NSLog
with。例如:

NSLog(@"Value of property : %d", A4.weight)

另外,查看上面的示例代码,
price
在类级别是不可访问的,因为它是在
func paperPrice
的范围内定义的。您可能需要定义
price
,类似于
weight
等,并使用
A4.price
进行访问

println
运行良好,您的类符合
可打印的
协议。以这种方式实现您的类。不要忘记添加
说明
属性

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))"
    }
}

你的paperPrice函数没有任何作用。它是为函数中定义的变量设置一个值。函数一完成,变量就超出范围。您需要将变量返回到类外的某个对象才能使用它。试试这个:

// 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)

确保计算paperPrice的函数返回可打印的值

return price