Coding Explorer Blog

Exploring how to code for iOS in Swift and Objective-C

  • Home
  • Apps
  • About
  • Contact

Println Is Now Print, and Do-while Is Now Repeat-While in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

Println Is Now Print, and Do-while Is Now Repeat-While in Swift

There are a few differences from Swift 2 that probably don’t need their own post, but should be mentioned nonetheless because they do have significant implications.  This post will cover a few of those changes:  Print replacing Println and the Repeat-While loop replacing the Do-While loop.

Print Replaces Println in Swift 2

You may have noticed a difference in the prior article The Guard Statement in Swift in some of the conditionals, though since the difference only consists of a couple characters, maybe you didn’t.  What is that change?  The Swift 1’s println function was replaced with the “print” function.  In Swift 1, println would print a line of text, with a “newline” character at the end, so each call to println would be on a new line.  If you just wanted to print a line, but not have the automatically appended “newline” character (so additional calls to the function would keep printing on the same line), you would use “print”.  Perhaps people did not use Swift 1’s “print” much?  Either way, it was decided to take that function name for the more common println call.

What if you wanted to use Swift 1’s original “print” in your Swift 2 code?  It’s still there, but in a longer format.  In Swift 1.2, we had:

//Swift 1.2 Version

print(value: T)
print(value: T, &target: TargetStream)
println()
println(value: T)
println(value: T, &target: TargetStream)

Now, we have:

//Current Swift Version

print(items: Any...)
print(items: Any..., to: &TextOutputStream)
print(items: Any..., separator: String, terminator: String)
print(items: Any..., separator: String, terminator: String, to: &TextOutputStream)

So now, 6 functions have been consolidated into effectively 4.  If you want a line that automatically ends with a newline character, you use:

print("Something with its own line!")

While if you want it to not have the newline, you have to give it a blank string for the “terminator” property:

print("Something that shares ", terminator:"")
print("its line with others.", terminator:"")

Actually, these are implemented as only 2 functions in the standard library, the separator and terminator have default values set to their parameters.  If you omit them, you just get the print(items: Any…) form of the function.  Autocomplete lists it as 4, basically with no defaults listed, and all defaults listed, but technically, with all permutations of this, it could be considered up to 8 functions.

This form of the print function actually will try to print a string representable form of whatever is put into that “Any” variadic parameter, and separate them with the separator parameter.  The default separator is apparently a “space” character, but you can put whatever you want in there, like a hyphen, forward-slash, etc.

The TextOutputStream option lets you print the output elsewhere.  Currently, the only option I can see in the standard library is to output it to another string, like using that other string as a buffer.  For most situations though, you will probably be using just print(value: T) or print(items: Any…, separator: String, terminator: String).

It may be something small, but I think that it is a significant change.  If nothing else, your tests or debugging code that use println() or print(value: T, appendNewLine: Bool) will need to be updated.

The Repeat-While Loop

Swift has a whole new way of looping?  Nah, this version has been around for a while (a “while”, get it?).  Apple decided to take the keyword “do” , and use it as part of their new Error Handling frame work.  This kind of leaves the classic do-while loop in a bit of a bind.  Well, they decided to rename it, not only to use it elsewhere, but to make the intention clearer.  Some while/do-while loops can be quite large.  With a while loop, that isn’t so bad, because you can tell right at the top that it is a while loop, and it will keep running until a certain condition.

With the original do-while loop, it is a bit less clear.  You will see a very small “do” at the top of a code block, then all of the code, and WAY at the bottom you will see the “while” conditional.  The idea behind it is basically saying, “Do {this big block of code} while (this conditional is true)”.  That makes sense, but the main point of loops is usually to repeat things.  While the semantics of that statement make sense, “do” does not really imply repetition.  Hence the replacement of the “do” keyword in the do-while loop to “repeat”.

Now, that sentence goes “Repeat {this big block of code} while (this conditional is true)”.  Saying repeat makes it very clear that this will be repeated while the while-statement’s conditional is true.  It is also to make it a bit more obvious, because the word “repeat” is significantly longer than “do”, thus making it also easier to see at the top of a big block of code in comparison to its predecessor.

Otherwise though, the Repeat-While loop is EXACTLY the same as the do-while loop prior to Swift 2, and how it is in many other languages.  It is a while loop that will always run the block of code ONCE, and then check the conditional.  This is as opposed to a normal “while” loop, which will check the conditional first, and if it does not evaluate to true, then the block of code will be skipped.

So, like it’s predecessor, its syntax is simply:

var count = 0

repeat {
    print("SWIFT 2 IS GREAT!")
    count += 1
} while count <= 15

Where the “count” initialization and incrementing are just to give the conditional something to look at.

Conclusion

These aren’t earth shattering topics, but I thought they were worth a mention, especially with the println change.  I didn’t want to just silently change all println statements to print, and just assume everybody saw it.  Of course, past articles will be updated to the new print style in time, along with any other updates to make them conform with Swift 2’s syntax.

Anyway, we will cover the new Error handling in another post, to see just where the “do” keyword went.  Stay tuned for that one, it’ll be a doozy, and maybe even require multiple posts.

I hope you found this article helpful.  If you did, please don’t hesitate to share this post on Twitter or your social media of choice, every share helps.  Of course, if you have any questions, don’t hesitate to contact me on the Contact Page, or on Twitter @CodingExplorer, and I’ll see what I can do.  Thanks!

Filed Under: Swift Tagged With: Swift

Subscribe to the Coding Explorer Newsletter

* indicates required

Follow Us

Facebooktwitterrss

Recent Posts

  • Error Handling in Swift
  • Creating and Modifying a URL in your Swift App
  • Watch Connectivity in Swift — Application Context
  • watchOS Hello World App in Swift
  • API Availability Checking in Swift

Categories

  • Class Reference
  • General
  • Interface Builder
  • My Apps
  • Objective-C
  • Swift
  • Syntax
  • Tutorial
  • Uncategorized

Archives

  • May 2016
  • March 2016
  • February 2016
  • December 2015
  • July 2015
  • June 2015
  • April 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • January 2014
  • November 2013
  • September 2013
  • August 2013
  • July 2013
  • Terms Of Use
  • Privacy Policy
  • Affiliate Disclaimer

Subscribe to the Coding Explorer Newsletter

* indicates required

Copyright © 2025 Wayne Media LLC · Powered by Genesis Framework · WordPress