Show me a 'Hello, World!' in Swift

Discussion in 'Mac Programming' started by aaronvan, Jun 2, 2014.

  1. AntoineLec macrumors newbie

    Joined:
    Jul 31, 2012
    #2
    answer is on page 2 of the Swift Programming Language available on iBooks Store..
     
  2. int3 macrumors member

    int3

    Joined:
    May 19, 2014
    Location:
    Portugal
  3. grandM, Jun 2, 2014
    Last edited: Jun 2, 2014

    grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #4
    The declaration and initialization was something like
    var announcement = "Hello, world"

    Apparantly the variable knows it's a string because a string is being assigned to it. No semi colon, no *, no @

    Why does this remember me of visual basic a decade ago?
    Oh right, just putting it onto the screen is the java/Arduino command println.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    println is Java and Arduino (and probably many other languages) but not C. In C you use printf. Why they couldn't just use print like Python is anyones guess.

    No, if you want to see C, you should skip to the section on the numeric types. That looks like it has hardly changed since C.

    From what I've seen, it looks like Swift is Rust. Someone made that comment on Wikipedia (I'd never heard of Rust prior to it) and it looks like they're right - I don't see major differences between the two.
     
  5. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #6
    “println is a global function that prints a value, followed by a line break, to an appropriate output. If you are working in Xcode, for example, println prints its output in Xcode’s “console” pane. (A second function, print, performs the same task without appending a line break to the end of the value to be printed.)”

    Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/fi/jEUH0.l

    ----------

    Sorry to be picky, but "Hello, world" is a string :)
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    Not only that, but you should use "let", not "var". Goddamnit, this language was only revealed a few hours ago, and one of the first things they tell you is that you should be using constants as much as possible, and already you people are using variables where you should use constants. I've struggled mightily for the past few years trying to get people to use the "const" keyword in C/C++/Obj-C, but my efforts were in vain. I thought it was because const wasn't added until years after the first version of the language was unveiled. Now I see that it'll take more than it being there from the start for people to use it.

    Maybe the syntax needs to be easier. Maybe

    announcement "Hello World"

    should work the way

    let announcement = "Hello World"

    does, so that it'll be fewer characters to type and people will somehow think it's better as a result (when really it's better for an entirely unrelated reason... and probably less readable... but at least safer).
     
  7. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #8
    Of course, I was just mad when writing it

    ----------

    my apologies, I didn't get to the swift book yet. I've corrected the other mistakes, I feel stupid today.
     
  8. Ganesha macrumors regular

    Joined:
    Aug 4, 2009
    #9
    Force them to work on an embedded platform that has 16k flash and 128 bytes of ram. They will either get clever fast or declare you can't do anything useful without 4GB of ram.
     
  9. int3 macrumors member

    int3

    Joined:
    May 19, 2014
    Location:
    Portugal
    #10
    So far I know, const is used when you want that piece of memory to be const. var str="1234" is the same let str="1234". In memory, both have 5 bytes.
     
  10. mochatins macrumors newbie

    Joined:
    Aug 18, 2012
    #11
    It's important to specify constants because it allows the compiler to make optimizations and for the program to run more quickly.
     
  11. mrxak macrumors 68000

    Joined:
    Apr 16, 2004
    Location:
    Drifting through space in a broken escape pod
    #12
    They're making a big deal over safety in this language, too. The more tightly you control your code with stuff like constants the less likely you'll make a mistake.
     
  12. y0semite macrumors newbie

    Joined:
    Jun 2, 2014
    #13
    I have figured out how to compile the "hello world" in an xcode project, but how do I compile a single file? Either in xcode or at the command line.
     
  13. Reluctant Adept macrumors member

    Joined:
    Jan 30, 2009
    #14
    xcrun swift sourcefile.swift

    ... assuming you have Xcode 6 set as your default via xcode-select.
     
  14. aaronvan thread starter Suspended

    aaronvan

    Joined:
    Dec 21, 2011
    Location:
    República Cascadia
    #15
    Thanks, I just downloaded that free book.
     
  15. int3 macrumors member

    int3

    Joined:
    May 19, 2014
    Location:
    Portugal
    #16
    Xcode 6 isn't available for free? Only who is signed up on developer program is able to access that? :(
     
  16. AntoineLec macrumors newbie

    Joined:
    Jul 31, 2012
    #17

    XCode 6 *beta* is available for registered iOS and OS X developers. XCode in general (non beta) is available to others (hobbyist programmers for example), so you'll have to wait until the final version is released.
     
  17. Amazing Iceman macrumors 68040

    Amazing Iceman

    Joined:
    Nov 8, 2008
    Location:
    Florida, U.S.A.
    #18
    it's very strange, it doesn't forgive you if you don't leave spaces between the variable and equal sign and declaration, as in these examples.

    var test = 12 \\ no problem
    var test=12 \\ Editor shows an error and the interpreter does nothing.

    Also, in some cases, it is case-sensitive.
     
  18. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #19
    Played around with Swift a little today. Made some type mistakes on purpose to see what kinds of messages I'd get from the compiler. Believe me, some of them are very cryptic and non-helpful. It's going to take some experimentation if you make a type mistake.
     
  19. hiddenmarkov macrumors 6502a

    Joined:
    Mar 12, 2014
    Location:
    Japan
    #20
    bit draconian there...

    I know still new but any options to tone the rules down a bit?

    I know with perl you get a few "best practices" modules that are adjustable to be not be as strict as needed.

    I know good code format is fundamental but say doing a class assigment and its really late at night I know I don't follow best practices to the T all the time.

    If say R (what I am learning atm online) stopped working everytime I wrote say

    var<-123
    not var <- 123

    ....we'd have some issues at around midnight.
     
  20. 960design macrumors 68020

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
    #21
    Tell me you are kidding! I'll rip my eyes out looking for this in a couple thousand lines of code.

    Heading off to disprove this right now, so that I can keep my sanity.
     
  21. Amazing Iceman macrumors 68040

    Amazing Iceman

    Joined:
    Nov 8, 2008
    Location:
    Florida, U.S.A.
    #22
    Well,this is still beta. I really hope the Editor will automatically fix any spacing issues. Plus, it's not hard to detect, as it points to the line and word where the error is located.

    Another issue I found with Playground is that it uses a lot of resources because it's always active and analyzing everything you type in realtime, on the paranoid side. It may be better if there's a button or shortcut key we can push to manually ask it to start interpreting the code.

    I'm sure it will evolve from now until release day.
     
  22. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #23
    You'd hope so!

    I haven't seen what the list of allowed characters in a variable name are, but considering unicode characters and emoji are allowed, I really hope the equals sign isn't!

    You could really mess with the heads of fellow developers if it is!
     
  23. MeFromHere macrumors 6502

    Joined:
    Oct 11, 2012
    #24
    I've been tilting at the same windmill for years, with the same mostly disappointing results.

    Some C-family programmers look at "const" as extra work/extra typing with no benefit. They can always claim the program "works the same without const", so "const" is unnecessary. (Even thought it isn't always true in C++, these folks usually haven't mastered C++ well enough to use those parts.) They find (or claim to find) no value in helping the compiler catch foolish mistakes, and no value in better expressing the code's intent with "const".

    Sometimes they just don't know better; a subset of those can be trained to do better.

    Sometimes they LIKE making their code as obscure as possible as a twisted form of job security. Avoiding "const" is just one tool in their arsenal. They are usually masters of the entire Book of Foul Code Smells.

    Regardless of the cause, this kind of programmer is expensive. They make extra work (cleaning up after them). If they reach critical mass in a project, it's pretty likely to fail. This kind of programmer seems to thrive in the "anything goes" world of C. They usually HATE languages like Swift that are designed to suppress bad habits. They hate compiler warnings and ignore them when possible. They hate type checking. They will hate optionals in Swift, because they will be reminded of all the "it might not exist" bugs they've been leaving in their code for years. Etc., etc., etc.

    So I say "Hurrah Swift!". Anything that makes Code Slobs uncomfortable is fine with me. :)
     
  24. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #25
    Glad to hear I'm not alone. Optionals look really great. Should end up with a lot fewer NPEs at runtime, and without having to use ugly annotations everywhere to explicitly say whether things may or may not use null.

    A bigger issue, thinking about it, than people making variables of things that should be constant, is that people make macros of things that should be constants, and they try arguing that they're the exact same, or even better.

    If there are macros in Swift, I haven't reached that section of the manual yet. Hopefully they're hygenic if they're present.
     

Share This Page