Mobile
Article

An Introduction to Swift 2.0 for iOS and OSX

By Patrick Haralabidis

This article was updated in February 2016 to reflect changes in Swift 2.0

Swift is Apple’s new language option for programming native iOS applications. It complements Objective-C, and this will be the case for some time, now at version 2, there is no better time to learn the new language.

Objective-C developers will find a lot of similarities with additional features such as type inference, strong typing, no reliance on header files, generics and more.

In this tutorial I will show you how to get started developing iOS applications with Swift 2. I will show how to setup a development environment, look into language fundamentals and compare the syntax with Objective-C, JavaScript and C#. At the end of this tutorial you will have created a simple but complete iOS application using Swift.

There are many cases when Objective-C, C# and Swift share similar syntax, refer to this file for a complete reference.

Setting up Your Environment

As with Objective-C, Swift requires a current Mac and Xcode. At time of writing version 7.2 of Xcode is the latest and is downloaded from the Mac App store.

get Xcode

It’s a large download, about 2.5 GB, so may take a while. Once the download completes, open your Applications directory and launch Xcode. Install any additional components required when promoted. If you have a previous version of Xcode installed it will updated to the latest version.

You are now ready to start exploring and developing with Swift.

Language Fundamentals and Syntax

Let’s look at the language syntax, the differences to Objective-C and how it compares to other modern languages.

As it wont be possible to cover all aspects of Swift in depth in this tutorial, I will use simple examples that show high level concepts.

If you would like to follow along, open Xcode, select Get started with a Playground and type each example to see it in action.

Welcome to Xcode

You can download the code we will be trying from GitHub for quick reference.

Variables and Constants

To declare a variable in Swift use the keyword var:

var number = 1

In other languages:

Objective-C

int number = 1;

C#

var number = 1;

Javascript

var number = 1;

You can see how similar the Swift syntax is, the only difference is the lack of a semi-colon at the end of declarations.

The main difference with Objective-C is that you don’t have to define the variable type because Swift uses type inference and is able to understand that the variable is a number due to its initial value.

You are able to define the type if you wish:

var number: Int = 1

Let’s declare a string variable in Swift:

var language = "Swift"

Here the declaration in Swift looks a lot cleaner and almost identical to C# and JavaScript. How does this compare with Objective-C?

NSString *language = @"Swift";

With Swift there is no need to use memory pointers (*) or to prefix the string value with the @ symbol.

If you are working with the Foundation framework, you can use a Swift string in anyway you use a NSString and have access to the entire NSString API

Constants in Swift can be declared using the let keyword:

let language = "swift"

Or

let language: String = "Swift"

In other languages the same declaration looks like this:

Objective-C

NSString  *const language = @"Swift";

C#

const string language = Swift";

With Swift allowing for type inference when declaring constants.

Working with variables in Swift is no different from other languages, but it has an easy method for letting you add values in strings:

var designers = 4
var developers = 4

var teamSize = "The team has \(designers + developers) members"

Arrays

In Swift you create arrays and dictionaries using square brackets [ ] and access their elements by writing the index or key in those brackets.

var arr = ["first" , "second"]

In other languages the same declaration looks like this:

Objective-C

NSArray *arr = @[@"first", @"second"];

C#

var arr = new[] { "first", "second" };

JavaScript

var arr = ["first" , "second"];

You can access an item from the array using the index value:

var order = arr[0]

And set the value using:

arr[0] = "zero"

The code is the same in Javascript and C# but with a semi-colon at the end of the statement.

Objective-C

NSString *order = arr[0];

You can enumerate an array using a for item loop:

for item in arr {
    // do something
}

In other languages the same declaration looks like this:

Objective-C

for(NSString *item in arr)
{
    // do something
}

C#

foreach (var item in arr) {
    // do something
}

JavaScript

for (index = 0; index < order.length; ++index) {
     //do something
}

To add another item to the array, use the += operator:

arr += ["fourth"]

or the append function:

arr.append("fifth")

Dictionaries

Declare a dictionary in Swift by defining key-value pairs. When declaring an empty dictionary you have to define the type of the key and the value.

var dict = Dictionary<String, String>()

You can declare and assign values:

var dict = ["MEL": "Melbourne", "SYD": "Sydney"]

In other languages the same declaration looks like:

Objective-C

NSDictionary *dict = @{
    @"MEL" : @"Melbourne",
    @"SYD" : @"Sydney"
};

C#

var dict = new Dictionary<string, string>
{
    { "MEL", "Melbourne" },
    { "SYD", "Sydney" }
};

To access an item from a Dictionary use:

var entry = dict["MEL"]

In other languages the same declaration looks like:

Objective-C

NSString *entry = dict[@"MEL"];

C#

var entry = dict["MEL"];

To set or add an item to the Dictionary use:

dict["PER"] = "Perth"

In other languages the same declaration looks like:

Objective-C

dict[@"PER"] = @"Perth"

C#

dict["PER"] = "Perth";

To iterate over a dictionary use:

for (cityCode, cityName) in dict {
    print("\(cityCode) : \(cityName)")
}

The cityCode variable will contain the key and the cityName variable the value.

In other languages the same declaration looks like:

Objective-C

for (id key in dict) {
    NSLog(@"key: %@, value: %@", key, dict[key]);
}

C#

foreach(var item in dict) {
    var cityCode = item.Key;
    var cityName = item.Value;
}

Loops

You’ve seen how to create items, let’s have a look at how to loop through them.

Firstly, for loop syntax:

for var number = 1; number < 5; number++ {
    //do something
}

As expected, you specify a value and increment until the condition is met. Objective-C and C# syntax is almost identical and so is JavaScript, but it omits the variable type.

You can achieve the same result by using the Swift for in variant:

for number in 1...5{
    //do something
}

Swift creates the number variable and assigns a value automatically while iterating over the specified value. 1…5 is a closed range that includes the numbers from 1 to 5.

Using the array created earlier you can iterate over the cities in the array using this syntax.

for city in arr {
  print(city)
}

Swift provides a while and repeat loop with syntax almost identical to Objective-C, C# and JavaScript:

while number < 10
{
    print(number)
    number++
}

The variable after the while statement is a boolean and the code will execute when it evaluates to true.

The repeat loop behaves the same way but ensures that code executes at least once before the condition is evaluated.

var number  = 9
repeat {
   print(number)
   number++
}
while number<10

This example ensures that the number value is displayed before it’s increased and evaluated by the while statement.

Conditionals

Whilst loops control repetitive tasks in code, if and switch statements control flow.

The if syntax in Swift can have parenthesis, but are optional, so you may use any style you prefer.

if city == "MEL" {
   print("Melbourne")
}

or

if (city == "MEL") {
    print("Melbourne")
}

if statements can be followed by an else if or an else.

if city == "MEL" {
    print("Melbourne")
} else if city == "SYD" {
    print("Sydney")
} else {
    print("Perth")
}

Switch statements in Swift are followed by a case validation, but a break statement is not required as there is no implicit fall-through. This means that once a case has evaluated to true and executed, the next case will not be evaluated. But, a default action is required.

switch city {
    case "MEL":
        print("Melbourne")
    case "SYD":
        print("Sydney")
    default:
        print("Perth")
}

The case statement can contain multiple values separated by a comma or ranges. With Swift you can use NSString in a switch statement, which is not possible with Objective-C.

Functions

Functions are a large subject to cover, but basic knowledge is useful so you can declare and use them.

Declare a function in Swift using the keyword func:

func sayName() {
  print("Patrick")
}

Pass parameters within the parenthesis by specifying a variable name and the type.

func sayName(name: String) {
  print(name)
}

You can pass multiple parameters separated by a comma.

func sayName(name: String, lastName: String) {
  print("\(name) \(lastname)")
}

Declare functions that return results by adding an -> after the parameters and specifying the return type.

func createName(name: String, lastName: String) -> String {
   return "\(name) \(lastname)"
}

Creating an iOS Application with Swift

Learning a new programming language is best achieved by writing code and creating applications. So let’s create a simple iOS application using Swift.

The app will connect to TheMovieDB API, request a list of upcoming movies, download the JSON results, parse them in to Dictionaries and Arrays and then populate this data into a Table View.

To keep this tutorial simple, it will cover common tasks such as creating and connecting UI elements to code, working with multiple controllers, delegates and protocols and show how to perform asynchronous operations with Swift, something that the language excels at.

Before starting, register and request an application API key from themoviedb.org website. Sign up if you do not have an account already, open your account page and generate a new key from within the API section.

The final code for the application is on GitHub and I have created branches for each stage.

Let’s Get Started.

Open Xcode, create a new project, select a single view application in the iOS section and click next.

Single View App

I called my project UpcomingMovies and select Swift as the language.

New Application

Click next and select a location to save the project.

Now create a table to display application data by selecting the Main.storyboard file in Xcode and dragging a Table View object from the Object Library.

Insert Table

To access the table in code, you need to create a delegate, a data source and an outlet.

Select the storyboard file, hold control and click and drag from the Table View to the View Controller, selecting data source in the popup menu. Repeat the process and select the delegate option.

To create the outlet, select the View -> Assistant Editor -> Show assistant Editor menu item. You will see a new section that displays the code from the ViewControler.Swift file.

Select the table and this time hold control, then click and drag from the Table View to the code window, releasing under the class declaration.

Give the outlet a name, I used appTableView, and click connect.

Create an Outlet

There’s one final task left to complete the UI. This is to create a prototype cell that will contain the movie details text and will be re-used for every cell created in the table.

To achieve this, select the Table View and in the attributes inspector, change the number of prototype cells to 1. Then click in the cell, change the Style to Subtitle and give it an identifier of MovieResultsCell.

Create Prototype

Change Subtitle

Alright, time to have some fun with Swift.

First you have to configure the data source and data set connected earlier in the storyboard.

In ViewController.swift change the class from this:

class ViewController: UIViewController {

To this:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

Now implement the 2 new protocols added above by creating the required methods.

Add the following lines of code to the ViewController.swift file.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1;
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier: String = "MovieResultsCell"

        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)

        cell.textLabel?.text = "Test"
        cell.detailTextLabel?.text = "Test details"

        return cell
    }

The first method defines the number of rows in the table and the second displays the data.

You can remove the following code as it’s not necessary.

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

The ViewController.Swift code should look like this:

First phase of code overview

This completes phase 1, go ahead and run the project, you should see the following result.

Example of App created so far

It’s time to connect to our API and retrieve the results. This code should be generic and reusable. The aim is to pass a URL as a parameter, get the result, parse the JSON and return an array with the data.

This code then can be re-used for subsequent API calls to other URLs.

Create a new class by right clicking in the folder that contains the ViewController.swift in the navigation pane and selecting New File.

Create a new file

Select Cocoa Touch Class, click Next and give it a meaningful name, I used APIController. Leave the default options as below.

Create API Class

Open the file and enter the following code underneath the import UIKit line.

protocol APIControllerDelegate {
    func apiSucceededWithResults(results: NSArray)
    func apiFailedWithError(error: String)
}

This defines a protocol that will be implemented in ViewControler.swift and will receive the results once the API call has completed.

Let’s add the functions that will make the actual call and process the result.

Within the class decleration add the following code:

var delegate:APIControllerDelegate?

  func getAPIResults(urlString:String) {

      //The Url that will be called.
      let url = NSURL(string: urlString)
      //Create a request.
      let request = NSMutableURLRequest(URL:url!)
      //Sending Asynchronous request using NSURLSession.
      NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
          do {
              //Check that we have received data
              guard let data = data else {
                  self.delegate?.apiFailedWithError("ERROR: no data")
                  return
              }
              //Call the JSON serialisation method to generate array of results.
              self.generateResults(data)
          }
          }.resume()
  }

func generateResults(apiData: NSData)
    {
        do {
            //Serialise the api data into a json object
            let jsonResult = try NSJSONSerialization.JSONObjectWithData(apiData, options: .AllowFragments)
            //verify we can serialise the json object into a dictionary
            guard let jsonDictionary: NSDictionary = jsonResult as? NSDictionary else {
                self.delegate?.apiFailedWithError("ERROR: conversion from JSON failed")
                return
            }
            //Create an array of results
            let results: NSArray = jsonDictionary["results"] as! NSArray
            //Use the completion handler to pass the results
            self.delegate?.apiSucceededWithResults(results)
        }
        catch {
            self.delegate?.apiFailedWithError("ERROR: conversion from JSON failed")
        }
    }

The first function takes a URL and makes a request to receive the data from the API. If there is an error it will execute the apiFailedwithError function of the protocol that passes the error string. On a Success it will call the generateResults function to parse the API data.

The APIController class should now look like this.

APIController

This completes Phase 2, if you build and run the project you will see that this has made no difference yet.

It’s time to populate the data in the table. Open ViewController.Swift and add the APIControler delegate to the Class:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, APIControllerDelegate {

Underneath the appTable outlet add the following 2 lines:

var searchResultsData: NSArray = []
  var api: APIController = APIController()

The first variable will contain the table data. The second variable creates an instance of the API controller that used to call any of its methods.

Now implement the API controller protocol by adding the following functions underneath the existing tableView ones. Make sure to replace the empty API key with your own.

  // MARK: APIControllerDelegate

  //Make the API call
  func getUpcomingMovies()
  {
      //Construct the API URL that you want to call
      let APIkey: String = "" //Replace with your Api Key"
      let APIBaseUrl: String = "https://api.themoviedb.org/3/movie/upcoming?api_key="
      let urlString:String = "\(APIBaseUrl)" + "\(APIkey)"

      //Call the API by using the delegate and passing the API url
      self.api.delegate = self
      api.getAPIResults(urlString)
  }

  //Handle the Error
  func apiFailedWithError(error: String) {
      let alertController = UIAlertController(title: "Error", message:
          error, preferredStyle: UIAlertControllerStyle.Alert)
      alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))

      self.presentViewController(alertController, animated: true, completion: nil)
  }
  //Handle the returned data
  func apiSucceededWithResults(results: NSArray) {
      self.searchResultsData = results
      self.appTableView.reloadData()
  }

The ViewController.swift should now look like this.

VCwithDelegates

Let’s change the tableView function and map the API data to the rows and cells.

Replace the following code

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier: String = "MovieResultsCell"   
      let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)

      cell.textLabel?.text = "Test"
      cell.detailTextLabel?.text = "Test details"

      return cell
  }

with this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      let cellIdentifier: String = "MovieResultsCell"

      let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)! as UITableViewCell

      //Create a variable that will contain the result data array item for each row
      let cellData: NSDictionary = self.searchResultsData[indexPath.row] as! NSDictionary
      //Assign and display the Title field
      cell.textLabel!.text = cellData["title"] as? String

      // Construct the posterUrl to get an image URL for the movie thumbnail
      let imgURL: NSURL = getPoster(cellData["poster_path"] as? String)
      // Download an NSData representation of the image at the URL
      let imgData: NSData = NSData(contentsOfURL: imgURL)!
      cell.imageView!.image = UIImage(data: imgData)

      // Get the release date string for display in the subtitle
      let releaseDate: String = cellData["release_date"]as! String

      cell.detailTextLabel!.text = releaseDate

      return cell
  }

Here values from the API are assigned to each cell, grabbing the movie title, the poster and the release date.

The API does not always return an image for the poster. This should be handled through the JSON serialisation in the APIController, but that approach is too complex to cover here. So for the purposes of this app, let’s use the following function:

func getPoster(posterPath: String?) ->NSURL
    {
        guard let posterPath = posterPath,
            let baseUrl: String = "http://image.tmdb.org/t/p/w300",
            let urlString: String = "\(baseUrl)" + "\(posterPath)",
            let imgURL: NSURL = NSURL(string: urlString)
        else {
            let defaultImageUrl: NSURL = NSURL(string: "https://assets.tmdb.org/images/logos/var_8_0_tmdb-logo-2_Bree.png")!
            return defaultImageUrl
        }
        return imgURL
    }

This checks if an image path is returned from the API and a URL constructed for the table. Otherwise the tmdb logo is displayed.

Replace the following function

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1;
    }

With:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchResultsData.count
    }

This will create as many rows in the table as the results from the API. Now that the table cells are ready to receive the data, it’s time to make the API call.

Add the following line into the viewDidLoad method

getUpcomingMovies()

The complete ViewController class should now look like this.

VC Completed

Before the application can run you need to enable ‘App Transport Security for non HTTPS connections’ because the tmdb API doesn’t provide encrypted data.

Right click on the info.plist file and select Open as Source Code.

enter image description here

Add the following code at the bottom of the file, before the closing dictionary tag.

  <key>NSAppTransportSecurity</key>
  <dict>
      <key>NSAllowsArbitraryLoads</key><true/>
  </dict>

Intro.Plist

That’s it, now run the application, which should display all upcoming movies.

Final App

I hope you have enjoyed this ‘swift’ tour through Apple’s new language and welcome your comments and questions below. If you’re keen to learn more then I recommend SitePoint’s Hello World tutorial and Swift video series.

Patrick Haralabidis
Meet the author
Patrick is a senior software developer at NAB in Melbourne. He is a certified .NET developer and has over 12 years of experience in application development. He is passionate about everything related to mobile and web and spends most of his time developing mobile applications on the iOS and Windows Phone platforms.
  • ElDerecho

    I am really hope they port it, or allow it to be ported, to other platforms. The language looks like it strikes a great balance between easy-to-learn scripting languages and native code compiled languages. And I am far from an Apple fan. But they did a good job on Swift.

    • KathrynDBradsher

      Start working at home>>CLICK NEXT TAB FOR MORE INFO AND HELP

  • vali nagacevschi

    Hi,

    Nice tutorial! Great job. I followed to letter and it compiles ok.
    But when running, I get exc_bad_instruction exc_i386_invop on line:

    let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell

    Don’t know how to move forward. Is there any param to change?
    Thanks,

    • Patrick Haralabidis

      Hi Vali,

      Thank you, I’m glad you liked it.

      This is a very generic error code and doesn’t help much.
      I have seen this happen when you have no data to populate the cells.

      Have you added your API key in this empty variable? var APIkey: String = “”
      if so, is your Mac connected to the internet to grab the data from the API?

      If neither of the above is the issue, please have a look at the Final branch on the GitHub repo and check if you have missed something.

      https://github.com/sitepoint-examples/UpcomingMovies/tree/Final

    • Chris Ward

      Hey guys, I also got the code to compile and run, but being a beta Xcode does crash occasionally etc etc…

  • Guilherme Akio Sakae

    There’s any change you fix the github repo? it’s blank now.

    I’ve found some errors when I try to compile the app, like the ViewController does not perform to UITableViewDataSource. When I change the UIViewController to UITableViewController I can compile, but when I try to add the protocol I get the same error but now with the new protocol.

    Maybe they have changed somethings in the xcode beta3 (version that I’m using)

    • Patrick Haralabidis

      Hi Guilherme,

      The master branch in the repo is empty but I have created branches for each stage of the tutorial.
      Please have a look at the Final branch for the complete solution.

      https://github.com/sitepoint-examples/UpcomingMovies/tree/Final

      The solution in the repo is written against Xcode beta3.
      What is the error that you are getting?

  • vali nagacevschi

    Hi Patric,

    Thanks for the reply. That was also my first guess and I checked from the browser the link with my api key.
    I got a json with the list of movies which I suspect is correct.
    I’ll look at the repo to see what I missed.
    Thanks,

  • Chris Ward

    A very timely blog post from Apple – https://developer.apple.com/swift/blog/

  • Patrick Haralabidis

    Xcode 6 beta is now available to all registered Apple developers. You do not need to be a member of the iOS or Mac developer program in order to download and use it.

    https://developer.apple.com/news/?id=07112014a

  • Jaycee

    hi, I compiled the first set of code where it need to generatea just a blanck table..It says
    unable to run in simulator..An error was encountered while running (Domain = FBSSystemServiceDomain, Code = 4)

    • Patrick Haralabidis

      Hi,
      It’s an issue with the beta version, try to quit and restart the simulator and Xcode.

      That worked for me.

  • http://www.mahurangi.school.nz Vern Dempster

    Hi. Thanks for the great introduction. However I cannot get past 1st base on the tutorial as I have followed your instructions. I dragged a table view object but do not get the menu on the left like yours and am unable to click and drag from tableview to the view controller. I can get a popup menu but unable to drag from there to anywhere other than itself. Sorry I am not explaining this well. Any help would be greatly accepted.

    • Patrick Haralabidis

      Hi Vern,

      If this is the first time you started Xcode it may not look like mine in the screenshots as some windows are closed.
      If I understand your problem correctly, you want to link the table view to the controller but cannot see the document outline to the left.

      To see the document outline, click on “editor” on the Xcode menu bar at the top, then select “Show Document Outline”.
      Alternatively, you can hold control, and then click and drag from the tableview to the yellow controller icon directly above.

      • http://www.mahurangi.school.nz Vern Dempster

        Thanks, “Show Document Outline” solved it as well as me realising I should drag a table view and not a table view controller into the default view controller( which it won’t do haha). Also in safari line 19 of the APIcontroller shows as
        if jsonResult.count>0 && jsonResult[“results”].count > 0 {

        and I had to get rid of the 2x amp;. and it all worked. Many thanks

  • J Soho

    Perhaps I already have a newer version of Swift / XCode (by the looks of the screenshots) and hence this difference: for me the app shows no images. This is because var urlString concatenates a String with an interpolated optional String, which results in strings like: “http://image.tmdb.org/t/p/w300 Optional(“someimg.jpg”)”

    To make this work I did:

    var movieUrlString: String? = cellData[“poster_path”] as? String

    if movieUrlString {

    var urlString: String = “(baseUrl)” + “(movieUrlString!)”

    var imgURL: NSURL = NSURL(string: urlString)

    var imgData: NSData = NSData(contentsOfURL: imgURL)

    cell.imageView.image = UIImage(data: imgData)

    }

  • Fernando Serapio

    I followed the tutotial step by step and xcode throws me a error “fatal error: unexpectedly found nil while unwrapping an Optional value” in this line:

    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell

    this is my first time on xcode and ios development, i have seen in the status of the API in my profile that that the app is actually calling to it

    • Patrick Haralabidis

      Hi Fernando,

      I’m not sure if this is cause by a different or newer version of Xcode.

      This issue normally occurs when you have no data to populate the cells.
      Have you verified that there is data being returned from the API call?

      To verify, put a breakpoint in this line in the APIController
      if jsonResult.count>0 && jsonResult[“results”].count > 0 {

      and check the data that is returned in the JsonResult.

      If you have null or empty values, add a check with an if statement in the tableView function following the great example that J Soho posted here.

  • David N. Brett

    Hi, and thanks for this tutorial !
    Had to modify the tableview functions to get it working.

    Replaced :
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
    By :
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

    And :
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
    by :
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

  • chan

    hi ~ thank you your tutorial~
    In xcode version 6.0, I implemented this code.

    Error Message >>> Type ‘ViewController’ does not conform to protocol ‘UITableViewDataSource’

    how do i sovle this problem?

  • Rudy Folden

    Just what I was looking for. Unfortunately, the code does not compile with XCode version 6.1 (the latest).

  • patrickcole

    Thanks for the write up but this code does not work with version 6.1.1 of Xcode. Can you please update this or point us to an update?

  • http://www.ulanjono.be Ivo Boon

    It seems that XCode 6.3 behaves not exactly as described in this tutorial. Since this is my first contact with Swift-XCode, I am overwelmed by everything and find it difficult to follow. After a few initial steps I get errors which I don’t understand.
    Couldn’t Sitepoint make a book like “Build your own website the right way” which learned me HTML and CSS and call it “Build your own iOS app the right way” learning Swift and Xcode?
    I can feel that Swift is a great language and XCode a terrific workbench, but I need to go forward one step at a time and not run from the beginning.

    • Chris Ward

      Hmm, Swift is still in beta and changes all the time, as does XCode. Do you have any specific error messages or problems? We’ll see what we can do to get the article updated.

      • http://www.ulanjono.be Ivo Boon

        I think this tutorial goes a little bit too fast for me…
        I restarted all over and after typing the first series of Swift statements in ViewController.swift, I get these errors:
        – class ‘ViewController’ has no initializers
        – ‘IBOutlet’ property has non-optional type ‘UITableView’
        and on the line of func tableView: Use of undeclared type ‘UITableView’
        I could send you a screenshot but this post doesn’t seem to accept attachments.

  • fbara

    Any chance this can be updated for Swift 2.0? I’m having some syntax problems with the code and I don’t know enough about Swift to figure out the solutions.

    • Chris Ward

      I’ll see if I can find a writer to update this…

      • Patrick Haralabidis

        Hi Chris,
        I can update this to swift 2.0, so that it becomes current again.

        • Chris Ward

          I wasn’t sure you were still active :)

          Message me and we’ll organise this.

          • fbara

            Thank you very much, gentlemen! I’m having a difficult time making the transition from Obj-C to Swift so any help you can provide, like this site, is beneficial.

  • Anastasija Mensikova

    This is amazing, but could you please update it for Swift 2.0 and iOS 9? I’m new to Swift and really need to use TheMovieDB urgently, and I’m struggling so much trying to fix all the errors this gives. Thank you in advance!

    • Chris Ward

      Yes, sorry for the delay, but we will have an update very soon.

  • Shiny

    Hi, in the reference document : Swift Syntax compared with Objective-C, C# and JavaScript
    The javascript function declare syntax is wrong.
    It should be “function”, not “func”

    • Chris Ward

      Thanks for that, this article was an update and this document has been around for a while, I’ll see if I can get it updated.

  • Richard Leandro

    I want have a macbook air for programming in Swift :'( , but in my country is so very expensive. –‘

    • Aleksander Koko

      You dont. Now you can code in Swift (but not for IOS) even on Linux. Check https://swift.org/

      • Richard Leandro

        Oh Thanks !

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in Mobile, once a week, for free.