Print Hello World in Swift

Updated on September 27, 2016

Introduction

Swift is a new programming language developed by Apple to develop MacOS and iOS apps. It was designed to be easier to learn as compared to Objective C and Apple sees it as a way to make it easier to get into app development for the MacOS and iOS platform as Swift was designed from the ground up to be an easier but powerful language to learn than the C like Objective C language.

Creating Apps versus Writing a Program

While generally you can’t create Mac OS or iOS apps without knowing how to program, you won’t necessary be able to create apps even after learning Swift. Swift is a programming language with standard inputs and standard outputs, namely keyboard, screen display, and the file system. The programs you will write in Swift will display in a console environment accepting input from the keyboard, and can read or write to files on the local hard disk.

To write Mac OS applications with nice user interfaces, we need to make use of the Cocoa API library.

To write iOS apps with nice user interfaces and touch input, we need to make use of the Cocoa Touch API library.

API stands for Application Programming Interface. It is basically a definition of application building blocks for developers to use to write specific types of applications quickly. The building blocks includes functions, data structures, object classes and variables.

A Software Development Kit (SDK) for a particular API needs to be downloaded to your development environment so that you can make use of the APIs. Currently the Mac OS and iOS SDKs are compatible with Objective C and Swift.

When you download and install the latest Swift, the latest SDKs for both the iOS and Mac OS are installed with Swift.

Writing Games

One of the more common reasons why people want to learn how to program is to be able to write their own games.

To be able to write games, obviously you need to learn how to program, and then on the Mac OS or iOS platform, you will need to learn how to make use of the Cocoa and Cocoa Touch library respectively and use the available graphical and animation functions to help write your game.

However, to make it slightly easier to write games, there are many game engines out there available for Mac OS and iOS that you can use so that you don’t have to re-invent the wheel when it comes to adding sounds, doing animations, or doing collision detections.

An example of a game engine is Cocos2d (http://www.cocos2d-x.org). You will need to download its SDK and learn how to make use of its API to be able to make use of its functionality. Cocos2d is a cross platform game engine.

There are many open source and commercial game engines which can be used.

Apple provides SpriteKit, which is a graphics rendering and animation infrastructure.

Xcode

Xcode is the name of the development environment for Mac OS and iOS apps. It currently only runs on MacOS. This can be downloaded from the App Store. This needs to be installed first before we can begin programming in Swift.

After you have installed Xcode, you can launch it just to check that it’s working. Since Swift is constantly evolving because it’s a new language , be sure to check the App store regularly to update Xcode.

For now, exit out of Xcode as we are going to be using REPL.

REPL

REPL stands for Read-Eval-Print-Loop. It is a command line interactive environment that you can use Swift commands. This is an interpreted environment meaning each Swift command gets actioned immediately much like the old BASIC command line interpreters and Python command environments. This provides a good way of getting immediate feedback on Swift commands and very useful for beginners when first learning the language.

Once Xcode have been installed, REPL can now be launched from the Mac Terminal app.

To launch the Mac Terminal app, navigate to the Applications, Utilities folder. Double-click on the Terminal app.

Ensure you are using REPL with the Latest Swift version installed

At the Terminal prompt, type in the following to ensure that you select the latest Swift version installed to be used. As you update Xcode from time to time, new versions of Swift get installed. The following command ensures that REPL will use the latest Swift version installed. You would only type the following once. The only other time you would need to type it in again is if you have updated Xcode.

sudo xcode-select –s /Applications/Xcode.app/Contents/Developer/

It will prompt for a password. This is the administrator password to your Mac.

Running the REPL environment

xcrun swift

If you are prompted to enter a password, just type in the administrator password to your Mac.

What you should now see at the end of the messages that may have scrolled past is:

Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance.

1>

This is the REPL environment. We’re currently using Swift version 2.2.

Type in :help at the 1> prompt and hit Enter.

1> :help

This will list all the commands available to use in the REPL environment

To exit out of the REPL mode, we would just need to type in :exit or :quit and hit Enter.

1> :exit

Print “Hello World”

At the beginning of learning a new programming language, it is customary to use the language to print Hello World to the standard output.

At the 1> prompt, type in print (“Hello World”) and hit Enter.

1> print("Hello World")

print is a Swift function which accepts a parameter and displays it to the default output.

Swift functions make use of the braces () to accept their parameters. In this case, the “Hello World” message was passed to print inside the braces.

Conclusion

Printing Hello World in Swift in REPL or as a console application is relatively simple. My next tutorial shows how to print Hello World to the iPhone using Swift.

Print Hello World to the iPhone using Swift

Comments

    0 of 8192 characters used
    Post Comment

    • kordahitech profile image

      Kordahi Technologies 

      2 years ago from 3 Forcados Road, Apapa, Lagos, Nigeria, 101254

      Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. for macOS, iOS, tvOS, watchOS, and Linux. Swift is designed to work with Apple's Cocoa and Cocoa Touch frameworks and the large body of extant Objective-C (ObjC) code written for Apple products. As its only three words to print. but its not that easy as other language syntax.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, turbofuture.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://turbofuture.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)