Swift print, println, NSLog

Swift – print, println, NSLog

Written By: Debasis Das (21-Mar-2015)
We can use print, println and NSLog in Swift but before we start into sample code lets understand the difference of each

Swift println vs Swift print

  • Using println will add a newline at the end of the results, whereas print will not.

NSLog vs println

  • NSLog is slower
  • NSLog adds a timestamp and identifier to the output, whereas println will not;
  • NSLog synchronizes the log statements so that if you’re issuing logs from different threads concurrently, they won’t overlap with each other; println can result in jumbled output if performed simultaneously from separate threads without doing some synchronization (e.g. dispatching it to some serial queue, such as the main queue);
  • When performed on physical device, NSLog statements appear in the device’s console whereas println only appears in the debugger console.

Swift – Printing a String

        let firstName = "John"

Swift – Print using string interpolation

        let age = 10
        println("\(firstName)'s age is \(age)")
        //John's age is 10

Swift – Printing an Array

        //Printing an array
        let nameArray = ["john","jane","mary","bill"]
        //[john, jane, mary, bill]
        Printing an element of an array
        String interpolation with an array element
        println("The 3rd element of nameArray is \(nameArray[3])")
        //The 3rd element of nameArray is bill

Swift – Printing float and Formatter float

        let salary = 1000.327
        println(salary)                 //1000.327
        println(String(format: "Salary = %.5f", salary)) //Salary = 1000.32700
        NSLog("Salary = %f", salary)    //Salary = 1000.327000
        NSLog("Salary = %2.2f", salary) //Salary = 1000.33

Swift – printing an int as string

        let experience = 10.5
        println(experience)     //10.5

Swift – Printing an array while maintaining the double quotes. Using debugPrintln

        let countryArray = ["USA","BRAZIL","MEXICO","CANADA"]
        //["USA", "BRAZIL", "MEXICO", "CANADA"]

Swift – print vs println 

        //print vs println in Swift
        //In the below case it will continue printing in the same line

Swift – printing the objects of a String Array

        var strArray = ["A","B","C","D","E"]
        for str in strArray{
        print(strArray) //[A, B, C, D, E]
        debugPrintln(strArray) //["A", "B", "C", "D", "E"]

Swift – Printing a Dictionary

        var employeeDictionary: Dictionary <String,String> = ["firstName":"John","lastName":"Doe"]
        //Prints [firstName: John, lastName: Doe]
        //Printing an Element of a Dictionary
        let fName = employeeDictionary["firstName"]
        println("First Name is \(fName)")
        //First Name is Optional("John")
        println("First Name is \(fName!)")
        //First Name is John

Swift – Printing File Paths

        //Printing the document file path
        println(NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString)
        //  /Users/debasisdas/Documents
        println(NSSearchPathForDirectoriesInDomains(.ApplicationDirectory, .UserDomainMask, true)[0] as NSString)
        //  /Users/debasisdas/Applications

Swift – Printing using ASCII

        //Printing Hebrew
        var content:String="\u{05D0} - aleph is a silent consonant"
        var content1:String="\u{200E}\u{05D0} - aleph is a silent consonant"

Swift – Printing Date

        //Printing Date, Printing Date Description With Locale
        let myDateDescription = NSDate().description // "2015-01-13 18:57:15 +0000"
        // you can also use it with current locale
        let myDateDescriptionWithLocale = NSDate().descriptionWithLocale(NSLocale.currentLocale())! 
        println(myDateDescription) //2015-03-22 00:44:48 +0000
        println(myDateDescriptionWithLocale) //Saturday, March 21, 2015 at 5:44:48 PM Pacific Daylight Time

Swift – Printing Elements of a Tuple

        //Printing Tuple
        //Unnamed Tuple
        let salesAndProfitTuple = (104.00, 15.19)
        println(salesAndProfitTuple.0) //104.0
        println(salesAndProfitTuple.1) //15.19
        //Named Tuple
        let salesAndProfitTupleNamed = (sales:400.00, profit:25.19)
        println("sales = \(salesAndProfitTupleNamed.sales)") //sales = 400.0
        println("Profit = \(salesAndProfitTupleNamed.profit)") //Profit = 25.19

Swift – Calling a function from a println Statement

        println("Greeting message is => " + greet("Good Morning !!! ", addressTo: "Mr Anderson"))
        //Greeting message is => Good Morning !!!  Mr Anderson

    func greet(message: String, addressTo: String) -> String {
        return "\(message) \(addressTo)"

Swift – Printing Variable and Object Types through Introspection

        var myvar0 = NSString() // Objective-C class
        var myvar1 = 42
        var myvar2 = "Hans"
        let myvar3 = NSDate();
        println( "TypeName0 = \(_stdlib_getTypeName(myvar0))")
        println( "TypeName1 = \(_stdlib_getTypeName(myvar1))")
        println( "TypeName2 = \(_stdlib_getTypeName(myvar2))")
        println( "TypeName3 = \(_stdlib_getTypeName(myvar3))")
            TypeName0 = NSString
            TypeName1 = _TtSi
            TypeName2 = _TtSS
            TypeName3 = NSDate

        //Print Swift Introspection
        var classname = reflect(myvar3).summary
        println(className)          //SwiftPrintTesting.AppDelegate
        println(myvar3.className)   //__NSDate


Posted in Cocoa, Cocoa Touch, iOS, Swift Tagged with: , , , , , , , ,
0 comments on “Swift print, println, NSLog
3 Pings/Trackbacks for "Swift print, println, NSLog"
  1. […] Also Read, Swift print, println and NSLog […]

  2. URL says:

    … [Trackback]

    […] Read More here: knowstack.com/swift-print-println-nslog/ […]

Leave a Reply

Your email address will not be published. Required fields are marked *