-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Learning Swift Second Edition
By :

If we try to call a function, such as normal, Swift is going to give us an error, as shown in the following example:
let repeated1 = repeatString("Hello", untilLongerThan: 20) // Error: Call can throw but is not market with 'try'
To eliminate this error, we must add the try
keyword before the call. However, before we move forward, I would recommend that you wrap all of your code inside a function, if you are following along in a playground. This is because throwing errors at the root level of a playground will not be handled properly and may even cause the playground to stop working. To wrap your code in a function, you can simply add the following code:
func main() { // The rest of your playground code } main()
This defines a function called main
that contains all the normal playground code that is called once, at the end of the playground.
Now, let's get back to using the try
keyword. There are actually three forms of it: try
, try?
, and try!
. Let's start by discussing the exclamation...
Change the font size
Change margin width
Change background colour