DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Swift for the Java Guy Part 1: Getting Started

Swift for the Java Guy Part 1: Getting Started

What can Swift do that Java cannot? Here are a few things to consider...

Julian Exenberger user avatar by
Julian Exenberger
·
Jun. 01, 16 · Opinion
Like (8)
Save
Tweet
Share
11.49K Views

Join the DZone community and get the full member experience.

Join For Free

It's been a while since I had to look at the Apple development Eco-system.

In fact the last time I did anything serious was back in 2009 when I tried to get in on the boom in apps at the time.

I honestly didn't have a lot of issues with Objective-C as a language per se. In fact if anything I much preferred that than if I had had to whack out code in C or C++.

I think my main objection—if I had one—was that the Xcode environment is nowhere close to the level of something like IntelliJ. Also the SDKs at the time were pretty limited compared to what you had on the full blown version of OSX.

Needless to say; I spent a lot of time doing a lot more boilerplate code than what I would have liked.

When Swift came out (almost two years ago now, can you believe it) I briefly looked at the language which I thought was quite nice but it didn't really interest me much. The biggest reason for this is that my world has been—for the most part—Server-side Java running on some type *nix based platform.

Fast forward two years and suddenly Swift is OSS and companies like IBM are trying hard to make Swift feature in my world.

Ok, but what can it do that Java cannot?

Well here's a few things to consider:

  • Docker is changing the landscape in terms of how we deploy software. In a lot of ways, Docker is the new Application server and the new virtual machine.
  • Together with Docker, we have the rise of Microservices and the need to create very small deployable units of code.
  • Swift is a native language, it's fast out the box and incurs very little overhead in terms of memory and startup time.
  • Swift is also a thoroughly modern and clean language.
  • The combination of these things potentially makes Swift a great platform to build Dockerised Microservices, perhaps better than a language which historically incurs a large cost with it's VM and dependent libraries.

Installing

If you are you are using OSX the easiest way to get running is simply to get the latest version of Xcode from the App Store.

If you are running Ubuntu (14.04 or 15.04) I suggest you use the excellent swiftenv utility to install and manage Swift versions.

You can also install swiftenv for OSX via Homebrew. This is useful if you want to easily play around with the Swift 3.0 development builds.

The system I am using for this post is running Ubuntu 14.04 and I using the 2016-05-09-a version which is a Swift 3.0 development snapshot. 

Firstly  install the swiftenv utility as follows:

git clone https://github.com/kylef/swiftenv.git ~/.swiftenv
echo 'export SWIFTENV_ROOT="$HOME/.swiftenv"' >> ~/.bashrc
echo 'export PATH="$SWIFTENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(swiftenv init -)"' >> ~/.bashrc

Then  go ahead and install the Swift development package.

swiftenv install DEVELOPMENT-SNAPSHOT-2016-05-09-a

That's it! Swift is now installed.

You can test the installation by running the Swift REPL

Building Your First Bit of Swift

My first issue that I encountered on Linux is the complete lack of a decent IDE for the Linux environment. For this example I ended up using good old Vim. 

However I'm—somewhat ironically—playing around with Visual Studio Code which also seems to do a decent job.

For this example I will do the obligatory "Hello world."

Simply create a file "HelloWorld.swift" and I put the following code in it.

print("Hello world")

And that's it. Swift will basically run any code which is declared inline a la Ruby, Python, Groovy, et al.

You can now run this as follows:

swift HelloWorld.swift

...at which point you see the output:

hello world

Wait a minute—didn't I say this is compiled!?

Another really neat feature of Swift is that you can run code both in interpreted and compiled mode. 

To compile the code you simply run:

swiftc HelloWorld.swift

This spits out the Executable "HelloWorld" which you can simply execute as:

./HelloWorld

And there you have it! Swift is up and running. In the next post we will explore the language a bit.

Some Notes

swiftc didn't work the first time on my machine. It turns out that Swift relies on a version 3.6 of Clang on Ubuntu 14.04

I fixed this by doing the following:

apt-get install clang-3.6
update-alternatives /usr/bin/clang clang /usr/bin/clang-3.6 100
update-alternatives /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100
Swift (programming language) Java (programming language)

Published at DZone with permission of , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • An Introduction to Data Mesh
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Top Five Tools for AI-based Test Automation
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: