Swift Xcode - new and frustrated

Discussion in 'iOS Programming' started by joetwizzy, May 19, 2016.

  1. joetwizzy macrumors member

    Joined:
    Sep 12, 2008
    #1
    Hi, I am semi new to programming and very new to swift.

    I paid for a course from Geeky Lemon which I find to be okay and I'm enjoying. Problem I'm getting is that occasionally if I do some code wrong and build the application it will crash, if I then remove the exact same code and rebuild it will still crash.

    I have managed to fix it before by removing a button and adding a new one.

    However on my latest project that didn't work. I deleted everything from the story board and re added it which still didn't work. The only way I could fix it was to start a brand new project, copy the code, then re add the elements (text field, image, button).

    So what this suggests is that something is being stored somewhere else that I'm not aware about. I have two projects now which to me appear to be identical but one works and one doesn't.

    I hope someone can help me out as it's getting very frustrating now the code is getting longer and I'm adding more elements to the story board, I've stopped learning now until I can fix this issue :(
     
  2. tyche macrumors 6502

    Joined:
    Jul 30, 2010
    #2
    I would suggest some things. First, slow down. If you're taking an online course there isn't a race. I don't know this course specifically but they usually throw a lot of things at you (not all of them correctly) and rush through the material - add this button, drag this, copy this code, etc... barely explaining anything. Pause often, double check your typing. Stop and understand what they are doing. Create your own separate project and use similar items you learned on your own to master what is happening.

    Secondly, crashes are fine and a great way to learn. Programmers with years of experience still get crashes. The difference is, they can read the error message and fix it.

    When your program crashes, look at the error message. It will 95% of the time tell you exactly what went wrong. There's a lot of information but usually right at the top it will say why it crashed. Sometimes it's very clear and you know right away what when wrong, sometimes it may seem cryptic. For example it might say something like "
    nsunknownkeyexception" and maybe "setvalue forundefinedkey". You don't know what that is, so Google it and you're find lots of answers which should lead you to the answer. In this example, you might have deleted the code but the storyboard still has a ui element attached to the missing code. Look at the connections inspector (circle with the right arrow) and maybe you see an outlet with an error message.
     
  3. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #3
    I suspect that your storyboard still has some outlet connection that got removed from your source file. These are a real pain in the ass to catch or remember to do. I've spent hours trying to debug code only to find out that the outlet in the storyboard was pointing to nothing.
     
  4. mildocjr macrumors 65816

    #4
    Lol, Apple really needs to throw a warning over this, this bit me several times in the past.
    --- Post Merged, Jun 2, 2016 ---
    I found this out looking at an XCode video on Apple's developer resources, if you option+click the run button you'll get a list of debug options. In there you can choose one that resolves addresses, don't remember exactly where, and it will spit out more readable error messages. Used it once and now if I catch one of Apples famous cryptic error message, I just turn it on and it leads me to the point where I had the runtime error.

    Essentially it goes from "BAD_EXEC" ... to ... "could not write variable as it has already been removed from memory".
     
  5. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #5
    I have been a developer for years and I feel the same way.

    Swift really sucks. I mean, wow. It's HORRIFYING to work with. I have never seen a programming language with such an awful compiler and riddled with so many stupid bugs. It's infuriating.

    Right this very moment I am dealing with an Xcode bug where the compiler is throwing errors on code that is PERFECTLY VALID and ran, without ANY problems, just an hour ago. Now, after closing Xcode, installing a new cocoapod, without any other changes, it simply refuses to compile, and is throwing stupid errors. Even when I then remove the pod, it still refuses to compile, throwing senseless errors like this one:

    Code:
    var userData : [String : AnyObject]?
    
    [. . . ]
    
    if let platformId = userData?["account"]?["_id"] as? String {
       [. . .]
    }
    
    //Downcast from 'String?!' to 'String' only unwraps optionals...
    ^ It's actually throwing an error saying that is invalid....even though it is PERFECTLY FREAKING VALID and doesn't break any rules whatsoever. This very same code was working perfectly fine just 20 minutes ago.

    I am furious. I am genuinely considering switching back to using Objective-C because this is simply unacceptable. I have seen better, more stable software from MICROSOFT, and that's sad.
     
  6. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #6
    As others have stated it sounds like a storyboard error.

    To find these, click on your storyboard, then in the side panel (which may or may not be collapsed), right click on your view controller and you'll see a little connections window. This window will show errors if you're missing a connection. Missing connections WILL cause you to crash.

    These usually happen if you rename a method, or remove a button but keep the method.[​IMG]


    Notice in the black popup pane at the bottom there is a little yellow triangle. That's an error. It's because I renamed the method from "buttonPress" to "button" in the code. This will cause a crash when I run the program.

    Also notice on the right side in the code, there is a little empty circle beside the @IBAction func button(sender: AnyObject) line. This can be another indicator that you have a storyboard error. These circles are usually filled in whenever you have a correct connection.
    --- Post Merged, Jun 3, 2016 ---
    I couldn't agree more. It's an incredibly sloppy language that has terrible syntax. I seriously don't get how/why people enjoy Swift.
     
  7. Mascots macrumors 65816

    Mascots

    Joined:
    Sep 5, 2009
    #7
    Yay, let's turn another thread that generally deals with Swift into another Swift shtting ground!
     
  8. AdonisSMU, Jun 5, 2016
    Last edited: Jun 5, 2016

    AdonisSMU macrumors 603

    Joined:
    Oct 23, 2010
    #8
    I don't know what your trying to do other than cast a value in a dictionary as a String. Have you tried the constructor syntax?

    At first glance I think maybe it should be.... But in theory you code "should" work. You think there is an error elsewhere perhaps?

    Code:
    String(value)
    Or maybe a clean build and compile is in order. I know I had a similar issue Tuesday where my code didn't work with valid optional syntax. and I closed and opened the Xcode and then it worked just fine with the same exact code.

    I do agree that the optional syntax needs some more work. Specifically around easier handling of dictionaries. IMO this is gross. The language as a whole I love. ObjC would just let you write unsafe code you want to write and crash at runtime.

    Most people griping about SWIFT don't understand optionals. Optionals keep you program safe and predictable. Apple need to give better guidance on how to leverage them properly.

    No need to go hyperbolic. SWIFT is 2years old ObjC is like 30 years old yet it still has more warty behavior and implementation issues and C# is a decade old with its own warts.

    https://github.com/apple/swift-evolution
     
  9. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #9
    My code is fine. It looks like an updated cocoa pod caused the issue. Removing it completely fixed the issue, which is strange, because the compiler was acting as if the problems were in my code. Quite frustrating to be honest and a huge waste of time.

    I honestly preferred Objective-C in that regard, I very rarely saw bogus errors like that.

    It's not all bad though. My swift code is usually faster and 2x-3x smaller than my Objective-C versions of the same code, and it usually runs faster to boot.
     
  10. AdonisSMU macrumors 603

    Joined:
    Oct 23, 2010
    #10
    Really ouch... Cocoa pod was the issue... WOW! Okay then. Im stunned that would be a hard bug to find.
     
  11. mildocjr macrumors 65816

    #11
    Well it's more human readable for one. In some places it takes less line of code than it would in other languages. It's hard to come to the conclusion that Swift will suck since IMO it's still in beta. Swift 1.0 felt like an Alpha, it was sloppy in needing so much Obj-C to get things done, however, with Swift 2.0 a lot of the Obj-C calls were put into Swift methods and it feels a lot cleaner than it used to. With Swift 3.0 it's going to be pulling further away.

    The main problems with Swift at this point in time:
    • Compiler needs to be more optimized
    • Reliance on Obj-C
    • Lack of Swift Methods (still need to call Obj-C methods to do things such as working with Dates; loops back to previous bullet)
    Why Swift is a nice change up:
    • Swift methods are readable
    • For statements make more sense for beginners (code adoption)
    • Optionals are great, leaves for very safe code.

    Swift is missing a few things that I think would be important to have like BigInts, optimized GCD and LCM methods. GCD and LCM seem to be pretty much standard for other languages, which is why I still consider Swift to be a beta language.

    I am upset that they took away the ++ style operator, It's not much trouble to do += 1 but the previous is more universal and faster to type.
     
  12. smacrumon Suspended

    smacrumon

    Joined:
    Jan 15, 2016
    #12
    I can't provide a solution for this specific problem, but I can help give you an idea on how you can manage your projects.

    Running into bugs is a fairly common experience and sometimes it can be time-consuming to find the cause and correct it especially when you are new to a language. One suggestion I could make is that each time you make a change to your project and it tests successfully, you should make a complete copy of your entire project folder (i.e. duplicating your successful project).

    To make simple duplicates of your project, in the Apple Finder, find your project folder and select it, then select 'File' from the menu, then select 'Duplicate'. An exact copy of your project is made. That way, whenever you run into bugs and errors that you cannot resolve, you can go quickly back to a recent version that worked without issue. You can often then compare projects, comparing the one that worked and the one that is throwing up errors, helping you find what might be causing the issue if the error messages aren't so clear. You will end up with a long list of project folder duplicates which you can easily delete once you're confident with your current project. It also makes it easy to look back at your project as it has evolved over time.

    Project ABC
    Project ABC copy
    Project ABC copy 1
    Project ABC copy 2
    Project ABC copy 3
    Project ABC copy 4
    ...
    Project ABC copy 18
    Project ABC copy 19


    I guess in short, the advice is whenever you have a success, duplicate your success by backing up your project folder. It makes all the difference in my opinion.

    Good luck with it all.
     
  13. Mascots macrumors 65816

    Mascots

    Joined:
    Sep 5, 2009
    #13
    You'd have much much better progress using GIT.
     
  14. smacrumon Suspended

    smacrumon

    Joined:
    Jan 15, 2016
    #14
    I guess there's that and other similar version control, but I prefer the simplicity of a local version duplicate with additional periodic backups to the cloud and elsewhere.
     
  15. AdonisSMU macrumors 603

    Joined:
    Oct 23, 2010
    #15
    Umm why not just use git?

    I'm sorry but your project being on git can make it easier to give people access who need it so they can help you with the bug you are finding.
     
  16. smacrumon Suspended

    smacrumon

    Joined:
    Jan 15, 2016
    #16
    I guess we all have different workflows and preferences as to how we manage projects.
     
  17. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #17
    Git is way, way superior to just creating copies of the project, and uses practically zero disk space to boot. It's just simply better.

    If you're working on a project, even if it's just a personal project, you should really use git regardless.
     
  18. AdonisSMU macrumors 603

    Joined:
    Oct 23, 2010
    #18
    No. It's not about different workflows. This is a clear cut and dry case of you use git when you are doing development and want to be able to reliably go back to old versions of a project.
     
  19. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #19
    The only possible alternative I can think of is that you could do backups of a project using binary-diffs, which wouldn't ever duplicate anything. But that's basically just a less-functional version of git at that point.
     
  20. smacrumon Suspended

    smacrumon

    Joined:
    Jan 15, 2016
    #20
    I can already reliably go back to old versions of a project. No issue.
     
  21. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #21
    @smacrumon, using a real source control system is certainly better than what you describe. Xcode is designed to work with git and other source code control systems. You can get side-by-side diffs in Xcode of arbitrary project folders only with difficulty. I know, I used to work like that. Your system is only likely to work for a single developer. I don't see how it could work with multiple developers. Also, what happens when you're sure that things are working and you delete your old project snapshots and then you're wrong?

    Anyway, I don't mean to criticize. You should open your horizons and learn to use real source code control. I always use it, even on single developer projects. git is the current source code system du jour. If you use a free web host like bitbucket or gitlab.com you get offsite backups for free also.
     
  22. smacrumon Suspended

    smacrumon

    Joined:
    Jan 15, 2016
    #22
    Yes, I'm aware Xcode works with Git and "better" depends on what needs to be achieved. Old project snap shots are only deleted when they are no longer required, so that there isn't ever a case for wrongly deleting snapshots. Thanks for your perspective though.
     
  23. MarkCollette macrumors 68000

    MarkCollette

    Joined:
    Mar 6, 2003
    Location:
    Toronto, Canada
    #23
    Back to the original post, I would either use GIT to determine what changed from a working version to the current broken code, or use any other DIFF tool, like diffMerge, to compare the whole working folder versus the whole broken folder. Since storyboards are just XML files, one can then see the little differences in the storyboards that aren't always obvious from Interface Builder.
     
  24. crashoverride77 macrumors 65816

    Joined:
    Jan 27, 2014
    #24
    Terrible syntax? LMAO
    Swift has the easiest syntax by far, what are you guys talking about.
     

Share This Page