Sunday, December 28, 2014

Hello World! Build Your First App in Swift Using Xcode 6


Hello World! Build Your First App in Swift Using Xcode 6
Hello World! Build Your First App in Swift Using Xcode 6

Swift is an innovative new programming language for Cocoa and Cocoa Touch. Writing code is interactive and fun, the syntax is concise yet expressive, and apps run lightning-fast. Swift is ready for your next iOS and OS X project — or for addition into your current app — because Swift code works side-by-side with Objective-C.

Modern
Swift is the result of the latest research on programming languages, combined with decades of experience building Apple platforms. Named parameters brought forward from Objective-C are expressed in a clean syntax that makes APIs in Swift even easier to read and maintain.

Swift has many other features to make your code more expressive:

  • Closures unified with function pointers
  • Tuples and multiple return values
  • Generics
  • Fast and concise iteration over a range or collection
  • Structs that support methods, extensions, protocols.
  • Functional programming patterns, e.g.: map and filter


Designed for Safety
Swift eliminates entire classes of unsafe code. Variables are always initialized before use, arrays and integers are checked for overflow, and memory is managed automatically. Syntax is tuned to make it easy to define your intent — for example, simple three-character keywords define a variable (var) or constant (let).

The safe patterns in Swift are tuned for the powerful Cocoa and Cocoa Touch APIs. Understanding and properly handling cases where objects are nil is fundamental to the frameworks, and Swift code makes this extremely easy. Adding a single character can replace what used to be an entire line of code in Objective-C. This all works together to make building iOS and Mac apps easier and safer than ever before.

Fast and Powerful
From its earliest conception, Swift was built to be fast. Using the incredibly high-performance LLVM compiler, Swift code is transformed into optimized native code, tuned to get the most out of modern Mac, iPhone, and iPad hardware. The syntax and standard library have also been tuned to make the most obvious way to write your code also perform the best.

Swift is a successor to the C and Objective-C languages. It includes low-level primitives such as types, flow control, and operators. It also provides object-oriented features such as classes, protocols, and generics, giving Cocoa and Cocoa Touch developers the performance and power they demand.

The Hello World tutorial was the first programming article written for our iOS programming course. As Apple released Xcode 6, the tutorial is no longer up-to-date. We received quite a lot of emails about the tutorial update. So here you are. Instead rewriting the same tutorial in Objective-C, we’ll show you how to create the Hello World app in Swift. What’s more, we create a screencast for you.

If this is the first time you come across the tutorial, you may wonder why we teach you building a Hello World app. This programming tutorial is written for absolute beginners. We want to encourage you to learn programming. So the first app should be very simple. Despite its simplicity, the “Hello World” app serves a few purposes:


  • It gives you an overview about the syntax and structure of Swift, the new programming language of iOS.
  • It also gives you a basic introduction to the Xcode 6 environment. You’ll learn how to create a Xcode project and lay out your user interface using Storyboard. Even if you’ve used Xcode 5 before, you’ll learn what’s new in the latest version of Xcode.
  • You’ll learn how to compile a program, build the app and test it using the Simulator.
  • Lastly, it makes you think programming is not difficult. I don’t want to scare you away from learning programming. It’ll be fun.
let’s get started.

Getting Started with Objective-C for development iOS

Getting Started with Objective-C


Several years ago, on the 9th January of 2007, Steve Jobs presented the first device, based on iOS platform, – iPhone. Since then our world has become deeply fond of smart devices of Apple corporation and can not be imagined without them: iPhone, iPad, iPod

Programs created for the iPhone are written in Objective-C. Objective-C is often described as a strict superset of the C language. Others call it C with object oriented concepts applied. The first time I saw it, I have to say I felt like I had no idea what I was looking at. It doesn't look like any other programming language that I know. But after you get through your first few programs, you'll find it to be much easier to follow. Being a strict superset of C, if you already have algorithms that are written in C, you should be able to port them over to the iPhone. The following chart shows some expressions that mean close to the same thing in C and Objective-C:

C/C++Objective-C
#include "library.h"#import "library.h""
thisself
private:@private
protected:@protected
public:@public
Y = new MyClass();Y = [[MyClass alloc] init];
try, throw, catch, finally@try, @throw, @catch, @finally
Some expressions that mean the same (or almost the same) in C/C++ and Objective-C.

Classes

In Objective-C, you declare a class' interface separate from its implementation. You have probably seen a similar separation of interface and implementation in C++ programs where a class' interface is defined in a *.h file the implementation of which is in a *.cpp file. Likewise, in Objective-C, the interface is defined in a *.h file and its implementation in a *.m file.

Every class you create in Objective-C should either directly or indirectly derive from the NSObject base class. The base class doesn't look to do much at first glance, but it contains functionality that the runtime needs for interacting with the object. You don't want to have to implement this functionality yourself.

Methods, Messages, and Object Communication

Most object oriented material you come across will refer to messages being sent among objects. Most of the time, you'll see this implemented through methods. In most cases, the words method and message can be used interchangeably. Objective-C sticks with using the terminology "message". So I'll adhere to that standard within this document.

The syntax for sending a message (which means the same thing as "calling a method") is to enclose the name of the target object and the message to be passed within square brackets. So in C++/C#, you may have send a message using the following syntax:

Class Instantiation and Initialization

An instance of a class is instantiated using the alloc method of the class. alloc will reserve the memory needed for the class. After the memory is allocated, you'll want to initialize it. Every class that has instance members will need to have an init method defined.

Only 4 thing You Need to Begin iOS Programming

What You Need to Begin iOS Programming

1. Get a Mac
Yes, you need a Mac. It’s the basic requirement for iOS development. To develop an iPhone (or iPad) app, you need to first get a Mac with Intel-based processor running on Mac OS X version 10.8 (or up). Probably you still own a PC, the cheapest option is to purchase the Mac Mini. The retail price of the entry model is US$599 (if you purchase via Amazon, the used model starts at US$490). You can pair it with the monitor of your PC. The basic model of Mac mini comes with 2.3GHz dual-core Intel Core i5 processor and 4GB memory. It should be well enough to run the iOS development tool smoothly. Of course, if you have more budget, get the higher model or iMac with better processing power.

2. Register an Apple Developer Account
Don’t mix this up with the paid iOS Developer Program that we’re going to talk about in later section. Everyone can register as an Apple developer for free. By registering the developer account, you’re allowed to download Xcode, access documentation of the iOS SDK and other technical resources such as development videos.

You can go to Apple’s developer website for registration. The registration process is very straightforward. Simply create an Apple ID (if you don’t have) and fill in your personal profile.

Sign up App Development Account
3. Install Xcode
To start developing iPhone and iPad apps, Xcode is the only tool you need to download. Xcode is an integrated development environment (IDE) provided by Apple. Xcode provides everything you need to kick start your app development. It already bundles the latest version of iOS SDK (short for Software Development Kit), a built-in source code editor, graphic user interface (UI) editor, debugging tools and many more. Most importantly, Xcode comes with an iPhone (or iPad) simulator so you can test your app even without the physical devices.

Sample Xcode Interface
To download Xcode, launch Mac App Store on your Mac. If you’re using the latest version of Mac OS, you should be able to open the Mac App Store from the icon in the dock. In case you can’t find it, you may need to upgrade the Mac OS.

Mac App Store icon
In the Mac App Store, simply search “Xcode” and click “Free” button to download it.

Download Xcode From Mac App Store
Once you complete the installation process, you’ll find the Xcode folder in the Launchpad.

Xcode in Launchpad
At the time of this writing, the latest version of Xcode is 5.1.1, which adds the support of iOS 7.1. For the upcoming tutorials, they’ll be based on this version. Even you’ve installed Xcode before, I suggest you to upgrade to the latest version if you’re planning to follow our tutorials.

4. Enroll in iOS Developer Program (Optional)
A common question about developing iOS app is whether you need to enroll in the iOS Developer Program. The short answer is “optional”. As mentioned earlier, Xcode already includes a built-in iPhone and iPad simulator. You can develop and test out your app right on your Mac.

Without joining the iOS Developer Program, however, the simulator is the only mean to run your apps. You can’t deploy and test the app on your device. Needless to say, you’re not permitted to submit your app to App Store. In other words, you can’t sell your app!

The simulator is powerful, however, it doesn’t simulate all features of iPhone. For instance, it doesn’t come with the camera or video capture feature. So if you’re building a camera app, the only way is to test it on a real iOS device. In other words, you have to join the iOS Developer Program!

So should you enroll in the program now? The iOS Developer Program costs US$99 per year. If you’re a new comer and just start exploring iOS development, you can rely on the simulator to test out your app first. You can wait until you have a solid plan to distribute your apps on App Store before enrolling in the program.

That’s all for today. Take some time to register your developer account and install Xcode. Once you’re ready, check out the next post and start to build your first app.