Slashdot Mirror


A Beginner's Guide To Programming With Swift

Nerval's Lobster (2598977) writes Earlier this year, Apple executives unveiled Swift, which is meant to eventually replace Objective-C as the programming language of choice for Macs and iOS devices. Now that iOS 8's out, a lot of developers who build apps for Apple's platforms will likely give Swift a more intensive look. While Apple boasts that Swift makes programming easy, it'll take some time to learn how the language works. A new walkthrough by developer David Bolton shows how to build a very simple app in Swift, complete with project files (hosted on SourceForge) so you can follow along. A key takeaway: while some Swift features do make programming easier, there's definitely a learning curve here.

44 of 72 comments (clear)

  1. Swift name by Anonymous Coward · · Score: 1

    I thought it was talking about SWIFT the object store, part of Openstack. Apple should have called their language iSwift.

  2. Just what we needed... by __aaclcg7560 · · Score: 2

    Another language filled with adverbs.

    1. Re:Just what we needed... by MightyMartian · · Score: 3, Insightful

      Another attempt by a vendor to try to lock in software development and make cross platform development incredibly difficult by introducing a new language.

      Fuck, I do tire of the sociopathic tendencies of corporations.

      --
      The world's burning. Moped Jesus spotted on I50. Details at 11.
    2. Re:Just what we needed... by iluvcapra · · Score: 4, Insightful

      Thank god we have Android Dalvik, where I can use my existing Java ME codebase. Oh wait.

      We're going from Obj-C to Swift, this seems like a pretty lateral move from a "cross platform" perspective. I would have thought the Great Java Wars had taught everyone that true cross-platform development is a chimera that isn't worth either the vendor or developer's effort. Platform vendors compete on features -- cross platform is antithetical to competition on features.

      --
      Don't blame me, I voted for Baltar.
    3. Re:Just what we needed... by Capsaicin · · Score: 1

      Another attempt by a vendor to try to lock in software development and make cross platform development incredibly difficult by introducing a new language.

      ... because writing Objective-C Apps using the Cocoa object framework is the very model of cross-platform development.

      What surprises me is that it Apple until now to "make cross platform development incredibly difficult."

      --
      Better to be despised for too anxious apprehensions, than ruined by too confident a security. --Edmund Burke
    4. Re:Just what we needed... by Pseudonym · · Score: 1

      "We wouldn't have swallowed this at NeXT", said Ross perorally.

      --
      sub f{($f)=@_;print"$f(q{$f});";}f(q{sub f{($f)=@_;print"$f(q{$f});";}f});
    5. Re:Just what we needed... by slashdice · · Score: 1

      What I find interesting is the ability to create new operators. Not just overloading existing ones, but creating new ones that affect parsing -- prefix, postfix, or infix with custom precedence and associativity.

      Custom operators can begin with one of the ASCII characters [list of punctuation dice considers junk characters], or one of the Unicode characters defined in the grammar below. After the first character, combining Unicode characters are also allowed. You can also define custom operators as a sequence of two or more dots (for example, ....). Although you can define custom operators that contain a question mark character (?), they canÃ(TM)t consist of a single question mark character only.

      If nothing else, the javascript nee ruby nee php hipsters will give some great examples of why this is a bad idea!

      Looking forward to some great fart apps written in ascii art...

      --
      Copyright (c) 1990 - 2014 Dice. All rights reserved. Use of this comment is subject to certain Terms and Conditions.
    6. Re:Just what we needed... by irq-1 · · Score: 1

      I would have thought the Great Java Wars had taught everyone that true cross-platform development is a chimera that isn't worth either the vendor or developer's effort. Platform vendors compete on features -- cross platform is antithetical to competition on features.

      Remember when people called browsers "platforms" and the websites were "applications"? 20+ years of the web has shown that cross platform works, that what counts as a platform is malleable, and that the features companies compete over, change over time. Today in mobile language is a feature in contention, but if Cordova (et al.) become popular, Apple and Google will compete over different features.

    7. Re:Just what we needed... by iluvcapra · · Score: 1

      Whatever you say, AC. However, I'm totally not surprised by a C-level executive and "Architect"-with-a-capital-A extolling the virtues of Java, Web UIs and giving the old song and dance about cross-platform only being about "performance."

      Most future projects will be cross platform because user now don't know/care what platform your services run on...

      Application vendors desperately want the platform to be a dumb pipe that disappears in order to deliver the Pure Application Experience. Platform vendors desperately want apps to distinctively convey their platform's benefits.

      --
      Don't blame me, I voted for Baltar.
    8. Re:Just what we needed... by iluvcapra · · Score: 2

      From most perspectives Swift is ridiculously ugly and not very interesting.

      public interface Troll {
        void post();
      }
       
      public class Complaint implements Troll {
        @Override
        public void post() {
            System.out.println("Swift is ridiculously ugly, especially compared to Java!");
        }
      }
       
      public class TrollFactory {
        public Troll getTroll(String trollType){
            if(trollType == null){
              return null;
            }
            if(trollType.equalsIgnoreCase("COMPLAINT")){
              return new Complaint();
            }
            return null;
        }
      }

      --
      Don't blame me, I voted for Baltar.
  3. Yeah but does it run on by Spy+Handler · · Score: 2

    Linux? Or do I have to buy a mac.

  4. learning curve? by Anonymous Coward · · Score: 1

    >A key takeaway: while some Swift features do make programming easier, there's definitely a learning curve here.
    you don't say?! One would expect Apple had essentially done away with learning curves so that just about anyone and their uncle can write great apps in just a few minutes! Programming is so overrated, how hard can it be?

    1. Re:learning curve? by narcc · · Score: 2

      They did ... once upon a time. Remember HyperCard?

      I don't know why people are so desperate to believe that programming is difficult. How old were you when you (very likely on your own) learned to program? 8 or 9 years old?

      I know, I know, if we give the unwashed masses simple and powerful tools they'll write bad code. The horror. Better leave it to the professionals. Surely, they never produce unmaintainable garbage...

    2. Re:learning curve? by Dutch+Gun · · Score: 2

      Programming is sort of like cooking. Anyone can learn the basics and make some interesting and satisfying stuff - even an eight year old can to do some rudimentary stuff. I'd even venture to say that many people can make a living at it, as there are lots of jobs that don't involve doing incredibly demanding tasks - just basic production work. But there are also top tier jobs that require extremely experienced and talented professionals with years of study under their belt. Not everyone is suited for that job, but that's fine, since there are quite a bit fewer of them anyhow.

      In other words, there's nothing wrong with making tuna casserole (using Hypercard to create a simple database application), but don't try to kid yourself it's basically the same thing as creating a full course meal at a four-star restaurant (writing low-level, high-performance code in C).

      And, just like there are bad chefs to have no real business being in the kitchen, there are professional programmers who shouldn't be coding until they've polished up their skills or gained some more experience. The fact that there's so much badly written code out there seems to demonstrate that programming well is rather more difficult than many would like to admit.

      --
      Irony: Agile development has too much intertia to be abandoned now.
    3. Re:learning curve? by narcc · · Score: 2, Insightful

      That's delusional. I've been at this longer than a lot of users here have been alive, yet the only "complex" applications I've seen are either unnecessarily complex or complex for reasons completely unrelated to programming.

      It's not like cooking at all. That's just wishful thinking.

      Programming is easy. Ridiculously easy. You know this, I know this. Why hide it?

      Why not produce easy tools for non-programmers to use? We use programming languages to make writing software easier, after all. Why are modern languages and tools becoming increasingly more complex? Software isn't getting more complex in general, so why are our tools? What are we afraid will happen? That we'll lose our jobs? That people won't admire us for a skill easily attainable by young children?

      there's nothing wrong with making tuna casserole (using Hypercard to create a simple database application), but don't try to kid yourself it's basically the same thing as creating a full course meal at a four-star restaurant (writing low-level, high-performance code in C).

      But it is! The EXACT same skills and principles apply equally in both cases. (Note: This is not the case for cooking. Your analogy breaks immediately.) The only difference is that you need to know a few additional technical details to use C effectively. That's tangential to programming, no different than something like domain specific knowledge you'd need writing programs for use in different industries.

    4. Re:learning curve? by Dutch+Gun · · Score: 2

      I happen to have written large-scale commercial videogames in C++ for most of my career. I've worked with lots of people, many smarter than me, and I don't recall anyone talking about how their programming work was "ridiculously easy". I'm glad everything is so simple for you, but apparently we're not all so fortunate.

      --
      Irony: Agile development has too much intertia to be abandoned now.
    5. Re:learning curve? by brantondaveperson · · Score: 1

      Well, to be fair *writing code* is pretty easy. But designing complex systems for reliability and maintainability is not. I suppose the GP believes that programming is the former, whereas I would have thought that it's more of the latter.

    6. Re:learning curve? by Dutch+Gun · · Score: 1

      Yeah, well, of course "writing code" is easy, just like typing coherent sentences in English is rather easy. It doesn't mean it's easy to write a novel any more than it means "programming is easy". The difficulty scales along with the scope and complexity of what you're trying to accomplish, as with most things in life.

      Honestly, I was trying to exit the topic of conversation gracefully, because it didn't sound like he was open to any sort of reasonable discussion, and feels more like a trolling attempt than a conversation at this point - calling me "delusional", and implying I'm in denial, fearful of losing my job, and that a child could likely replace me at work. That doesn't seem like a conversation worth salvaging, but thanks for your reply.

      --
      Irony: Agile development has too much intertia to be abandoned now.
    7. Re:learning curve? by narcc · · Score: 2

      it didn't sound like he was open to any sort of reasonable discussion

      Well, you're right about that. A "reasonable discussion" isn't possible here. Can you have a "reasonable discussion" with a creationist? Neither can I. All you can do is show them the facts. It's up to them to accept reality, just as it's up to you.

      Just look at the parent here:

      designing complex systems for reliability and maintainability is not

      Ah, but it is! (Hell, if it's actually complex, it's not maintainable.) See, most "complex systems" are only complex because of how their designed (poorly). Yes, some things are actually complex but that's generally completely unrelated to programming (see my earlier post).

      Personally, I blame the OOP craze for the dramatic increase in unnecessary complexity. We've got two generations, now, of programmers who know nothing about modularity, but believe (inexplicably) that OOP gives them modularity for free.

      This shouldn't be controversial. The only explanation I have for the need to believe that programming is difficult is fear.

    8. Re:learning curve? by tehcyder · · Score: 1
      I'd rather have a good car analogy.

      Programming is like driving a car: some people are incapable of driving without killing bystanders or themselves, the majority can get by quite happily going shopping, and a few are F1 racing drivers.
      Most programs, and especially most iPhone apps, are my mum going to Tesco's, not Lewis Hamilton pipping Nico Rosberg by two hundredths of a second in the final title-deciding race of the season.

      --
      To have a right to do a thing is not at all the same as to be right in doing it
    9. Re:learning curve? by Tablizer · · Score: 1

      Learning programming is relatively easy. Learning to write maintainable code, on the other hand, takes skill and experience.

      Clerks and accountants used to do amazing things with Lotus-1-2-3 macros because Lotus cleverly leveraged what users already knew about spreadsheets into a Turing Complete set of commands. But often these users eventually got themselves into a jam, or made something that nobody else could decipher.

  5. Slight Misunderstanding by iluvcapra · · Score: 3, Informative

    TFA is actually mostly a Cocoa application stack guide. Discussion of the actual distinguishing features of Swift is minimal -- in fact I think the only thing they even passingly mention is unwrapping of Optionals. Otherwise it's just "How to build an iOS app"

    --
    Don't blame me, I voted for Baltar.
  6. Embracing the bird by javawocky · · Score: 1

    I have started going through the "Skip Wilson" tutorials and have found the language quite refreshing - borrowing mainly from JS / Java / Python imo. But then again anything more refreshing than my PHP day job.

    1. Re:Embracing the bird by goombah99 · · Score: 1, Funny

      I love his Geraldine character.

      --
      Some drink at the fountain of knowledge. Others just gargle.
    2. Re:Embracing the bird by MightyMartian · · Score: 4, Funny

      If you start with PHP, even Brainfuck looks fun and refreshing. Jesus pal, talk about damning a language with faint praise.

      --
      The world's burning. Moped Jesus spotted on I50. Details at 11.
    3. Re:Embracing the bird by HiThere · · Score: 1

      Now that's just not true. Brainfuck is worse than PHP. So, for that matter, is whitespace.

      --

      I think we've pushed this "anyone can grow up to be president" thing too far.
    4. Re:Embracing the bird by Paradise+Pete · · Score: 1, Insightful

      What are you, 60?

      Don't rag on people for getting old. That kinda is the goal, isn't it?

    5. Re:Embracing the bird by terjeber · · Score: 1

      exaggerating slightly for comic

  7. self linking content now? by dnebin · · Score: 1

    You typed "I love Dice".

    we're self linking now?

  8. Yeah but does it run on by slashdice · · Score: 2

    The 1.0 syntax was just barely frozen. There are a couple open source swift implementations (check github... you won't find them on source forge!) that are making progress. Not sure how useful it would be without a Cocoa/OpenStep library, though.

    --
    Copyright (c) 1990 - 2014 Dice. All rights reserved. Use of this comment is subject to certain Terms and Conditions.
  9. self linking content now? by slashdice · · Score: 1

    And hosted on source forge. Which makes it the first new source forge project in 6 months.

    --
    Copyright (c) 1990 - 2014 Dice. All rights reserved. Use of this comment is subject to certain Terms and Conditions.
  10. Re:Prerequisites by Anonymous Coward · · Score: 1

    "You need a Mac that can run OS X Mavericks"

      A key takeaway:No wonder Android has more Apps(sic) than iOS after starting from behind.

    Because your dev machine for this new language can't be more then 6 years old? Yeah, sure.

  11. Re:Prerequisites by Half-pint+HAL · · Score: 1

    "You need a Mac that can run OS X Mavericks"

    A key takeaway:No wonder Android has more Apps(sic) than iOS after starting from behind.

    Because your dev machine for this new language can't be more then 6 years old? Yeah, sure.

    No, because your dev machine for this new language has to run a particular operating system, and most indy devs won't throw out a grand for a machine with no guaranteed payback.

    --
    Got them moderator blues I blieve I walk out the do', With these mod-points I been gettin', I 'most never post no mo'
  12. SWIFT will be great for agile development ... by PPH · · Score: 2

    ... Tom said spryly.

    --
    Have gnu, will travel.
  13. YACC by callahan2211 · · Score: 1

    Yet Another Programming Language

    --
    "There are no gods, no devils, no angels, no heaven or hell. There is only our natural world. Religion is but myth and
  14. More Swift Shennanigans by Anonymous Coward · · Score: 1

    I've been learning Swift for the last few months. My last few years have been solid ActionScript 3 and I've really enjoyed the move. "Proper" strong typing, didSet observers, optionals, generics, and so on and so on are all a happy revelation to me.

    I'm now tinkering with everything from component creation to Grand Central Dispatch for threading to drawing Hermite spline curves to applying filters to images to fluid dynamics. If you're on the London Underground and see a chap frantically typing on a little Mac Book, that'll be me.

    I have a blog which is a diary of my progression in Swift, and might be a good resource for Swift newbies (especially those with an AS3 or Flex background). All my source code is open and available on GitHub: http://flexmonkey.blogspot.co.uk/search/label/swift

  15. Re:Prerequisites by iluvcapra · · Score: 1

    I'm uncertain of the statistics on this, but whenever I see photos of "Indy dev" conferences I always see a lot of silver laptops with glowing fruit. I think most of these people are already equipped.

    --
    Don't blame me, I voted for Baltar.
  16. Re:Prerequisites by Half-pint+HAL · · Score: 1

    Well here's a question for you then... do most indy devs go to indy dev conferences? I've never been to one....

    --
    Got them moderator blues I blieve I walk out the do', With these mod-points I been gettin', I 'most never post no mo'
  17. Re:Prerequisites by MaryAnnEvans · · Score: 1

    Android only has more apps if you count malware and crapware. The app store guidelines and the need to get past a human tester is far more of a restriction on app numbers than the need to have Mavericks. The number one reason for app rejections is the discovery of bugs by the approvals staff. Equivalently buggy apps will find their way into the Android stores without any barrier.

  18. Re:Prerequisites by MaryAnnEvans · · Score: 1

    Those that do go to conferences show themselves to be open to new ideas and to networking with other developers. Are they likely to be better or worse developers on average than those that don't go?

  19. Re:Prerequisites by Half-pint+HAL · · Score: 1

    I don't buy that logic. Some people who go to conferences will be posers who are still playing at being devs, but will never release a product.

    --
    Got them moderator blues I blieve I walk out the do', With these mod-points I been gettin', I 'most never post no mo'
  20. Re:Prerequisites by Half-pint+HAL · · Score: 1

    and most indy devs won't throw out a grand for a machine with no guaranteed payback.

    Good. With 1.3 million apps in the Apple App Store, there's enough already. Cutting out people who by their choice of PC show themselves to be less aware of good design is no bad thing.

    That, mate, is what we call "bigotry". There are many factors influencing choice of development machine. For the dev with a family, that includes paying for the kids' clothes.

    --
    Got them moderator blues I blieve I walk out the do', With these mod-points I been gettin', I 'most never post no mo'
  21. Re:Prerequisites by MaryAnnEvans · · Score: 1

    If your development income isn't paying for your development machine, then you're not a professional. I'd rather buy from professionals than amateurs.

  22. Re:Prerequisites by MaryAnnEvans · · Score: 1

    Note the words "on average". For sure there will be some people such as you describe. But their proportion is a hell of a lot higher amongst those who's only commitment was downloading an SDK.