There are mainly two functions available for printing the textual representation of objects and variables they are print( ) and println( ).
println( ) writes a newline character to the standard output.
Other functions available are debugPrintln() and debugPrint().
Examples of some functions are given below which can be used for advanced debugging.
println( ) writes a newline character to the standard output.
Other functions available are debugPrintln() and debugPrint().
Examples of some functions are given below which can be used for advanced debugging.
println("Hello World!")
Hello World!
var temp: String = "Test String"
println(temp)
debugPrintln(temp)
println(temp)
debugPrintln(temp)
Test String
"Test String"
For Customizing an object's textual representation we can override the methods debugDescription( ) or description( )
class ClassA: NSObject {
var name: String = "unknown"
override var description: String {
return "ClassA: \(name)"
}
}
var tempA: ClassA = ClassA()
print(tempA.description)
println(tempA)
var name: String = "unknown"
override var description: String {
return "ClassA: \(name)"
}
}
var tempA: ClassA = ClassA()
print(tempA.description)
println(tempA)
ClassA: unknown
ClassA: unknown
var str: String = toString(tempA)
println(str)
println(str)
ClassA: unknown
Use of precondition( ) and preconditionFailure( )
var value: String = ""
if value.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 0 {
preconditionFailure("value is empty")
}
if value.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 0 {
preconditionFailure("value is empty")
}
fatal error: value is empty: file /Users/cramesh/Documents/My_Works/SwiftTutorial/SwiftTutorial/AppDelegate.swift, line 37
println(unsafeAddressOf(value))
console output
0x00007f8df251e480
var className = NSStringFromClass(self.classForCoder)
println(className)
var methodName = NSStringFromSelector(__FUNCTION__)
println(methodName)
or
println(__FUNCTION__)
No comments:
Post a Comment