Resolved Swift language

Discussion in 'iOS Programming' started by Timerez, Jun 4, 2014.

  1. Timerez, Jun 4, 2014
    Last edited: Jun 4, 2014

    Timerez macrumors newbie

    Joined:
    Jun 4, 2014
    #1
    Hello,

    I'm new to this site and new to Objective-C/Swift both. I recently started using Swift yesterday via Xcode 6 beta. I know it's beta and I'm attempting to learn how to code in swift but xcode 6 keep crashing. I barely started and I only want to practice to "get it" as I code along.

    For example, I'd type in:

    Code:
    var first: Integer = 5
    println(first)
    
    var second: Double = 4.4
    println(second)
    
    let sum1 = Double(first) + second
    println(sum1)
    And boom, it crashes. And I see people are having no problem developing something (Googled and saw someone posting "recreated flappy bird with swift") so I assume it's just me. How do I find the cause of this particular problem and fix it?

    Also, my macbook pro is on OSX 10.9.3 and laptop itself is bit old, made in 2008. Is it the laptop or is it something else?

    Thanks!
     
  2. brianm801 macrumors member

    Joined:
    Mar 1, 2011
    Location:
    Herriman, UT
    #2
    No idea why your crashing... but i would recommend downloading the eBook:
    https://itunes.apple.com/us/book/id881256329?mt=11

    Swift uses 'type inference'... 98% of the time you don't have to do:
    PHP:
    var firstInteger 5
    Just do:

    PHP:
    var first 5
    The compiler will figure out its in Int.

    Also, make sure you know the difference between 'var' and 'let'.

    var = variable (can change)
    let = constant (can not change)

    var = mutable
    let = immutable

    example:
    PHP:
    var numbers = [5,9,12]
    let othernumbers = [3,7,11]

    numbers.append(2// this is ok!
    othernumbers.append(13// this will give an error
    Read Apple's eBook... Follow the examples and do the 'experiment' sections.
     
  3. Timerez thread starter macrumors newbie

    Joined:
    Jun 4, 2014
    #3
    1) I already read up to roughly first 100 pages of that ebook as you suggested. I mostly "get it" through reading.
    2) I already know how to code but in Java, picking this up along with Obj-C language right now for my internship.
    3) I already took note of the difference between let/var.
    4) I also already took note of "type interference" - and that is what I am attempting to experiment with.

    I basically am asking on how could I counter xcode 6 beta's frequent crashing. :(
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    I've noticed a rash of crashes while working on their experiments. I think we'll just have to wait for the next beta release of Xcode 6 and see if that fixes the problem. But if you have an example that always causes a crash, submit a bug report to Apple. This is a big reason they release betas.

    P.S. I get the following error from your "let sum1 = ..." line:
    Code:
    Playground execution failed: error: <REPL>:9:26: error: could not find an overload for '+' that accepts the supplied arguments
     
  5. Timerez, Jun 4, 2014
    Last edited: Jun 4, 2014

    Timerez thread starter macrumors newbie

    Joined:
    Jun 4, 2014
    #5
    Yeah, that's usually the problem but I thought I could make it work. In fact, I copied sample from ebook, it works. However, it won't work if I put 2 variables created together...

    1+1 = works
    var + 1 = works
    var + var = not working (even if I include datatype such as Double(var) + var)...
    Edit: also, tried with Double(var) + Double(var) just for the hell of it, and doesn't work. lol

    It's just bit odd. And I also crashes as soon as I try and make array too. But you might be right. I might have to wait for next patch for this beta. :(
     
  6. brianm801 macrumors member

    Joined:
    Mar 1, 2011
    Location:
    Herriman, UT
    #6
    I wrote a simple little app last night in swift/xcode6 with no crashing (current-gen mac book pro w/10.9.3) ... I did find a bunch of autocomplete errors that kept making me pull my hair out.. but no crash.
     
  7. Timerez thread starter macrumors newbie

    Joined:
    Jun 4, 2014
    #7
    Do you also have xcode 5 installed? (Dumb question but I want to be sure I don't assume)
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    I did some more fiddling around and found that the reason for the error is that you've declared first as an Integer. If you declare it as an Int, Playground reports no error. Seems you can't convert an Integer to a Double. I would avoid using Integer unless you have a very specific reason to. Apple wants you to use Int (and type inference even more). See the "Int" section in The Basics chapter for more info.
     
  9. Timerez, Jun 4, 2014
    Last edited: Jun 4, 2014

    Timerez thread starter macrumors newbie

    Joined:
    Jun 4, 2014
    #9
    Oh wow, I assumed that I was expected to type "Integer" instead of "Int" when declaring datatype. Trying my code again with your advice on avoiding Integer worked for me, and didn't crash the program. And I thought it was the program itself and not the way I wrote code... Thanks for your help. :) I suppose I have to be extra careful with declaring datatype.

    Edit: I forgot to ask a question: did your program crash when you used Integer in place of Int?
     
  10. DannyBres macrumors 65816

    DannyBres

    Joined:
    Oct 30, 2007
    Location:
    UK
    #10
    or do as apple intend you to do and do not declare the type!!
     
  11. Timerez thread starter macrumors newbie

    Joined:
    Jun 4, 2014
    #11
    If you had read my posts, you'd see that I'm only experimenting. I learn the best through experimenting and I was not sure if it was the program itself or way I wrote. I want to know as much as possible to impress my boss...

    Thanks for your advice.
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    It hasn't... yet. I did get frequent crashes while trying to do the "full deck of cards" experiment.
     
  13. Timerez thread starter macrumors newbie

    Joined:
    Jun 4, 2014
    #13
    I haven't gotten to that far in either Swift (obviously) or Obj-C, yet. I think I saw that demo in one of my Obj-C related books but haven't read into it. Anyway, thank you for your help on finding the cause of my frequent crashes. I think I'm going to close this post, if I can.
     
  14. dejo, Jun 4, 2014
    Last edited: Jun 4, 2014

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    Just mark the thread Resolved. Please and thank you.
     

Share This Page