Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Nermal

Moderator
Original poster
Staff member
Dec 7, 2002
21,194
4,852
New Zealand
Hi everyone,

I'm trying to learn iOS/Swift development (please assume that I know nothing!) and I need to connect to a REST/JSON Web service and pull back data. I've found a few samples online but most of them are in Objective-C, and the only Swift one that I've been able to find doesn't work.

I've been using this one http://network-development.blogspot.co.nz/2014/06/access-rest-web-service-with-apples-new.html and if I copy ITunesSearchAPI.swift without any changes and try to build then I get five errors and a warning (not copied verbatim):

  • stringByAddingPercentEscapesUsingEncoding is deprecated
  • Value of optional type 'NSURL?" not unwrapped
  • Value of optional type 'NSURLConnection?' not unwrapped
  • 'println' and been renamed to 'print' (x2)
  • Extra argument 'error' in call
I've tried to fix these one by one, but just end up with more and more errors (culminating in something to the effect of "init(request:delegate:startimmediately) was deprecated: Use NSURLSession", which means nothing to a newbie). Swift is a fairly new language; have things really changed that much since this example was published in 2014 or is the sample just broken?

More importantly, can someone please suggest a working sample?

Thanks :)
 
For sake of clarity, you technically are not the cause of those errors - so don't think it's anything you've done with what you're working with.

Instead, they are remnants of a previous form of Swift - when looking for resources, try and find ones focusing on Swift 2 (the current version). As you start to understand the constructs and Swift syntax, errors like that resulting from others' older works will become little more than an annoyance, rather than show stoppers.

Swift really has changed a lot since its introduction and will continue to have major changes, but its on an annual basis. Before continuing, though I suggest reading Apples Swift guide - it sounds like you still have quite a bit to grasp as far as the extent of the language and should get those grounded before experimenting with higher concepts.
For example, if you're unsure of what an Optional is, then you should stop now and read a little bit more about foundational pieces of the language that you should be aware of, Optionals included.
 
One of the 1st things I do when looking at a tutorial is to check the date. Then look at the language it's using and for clues in the syntax to see how up to date it is.

It sucks that so many are out dated, but after a while, you should be able to look at what they are trying to do and make it work in the modern code. You can think of it as a bonus exercise, as it can really test your ability to understand how things work under the hood :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.