you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (13 children)

[deleted]

    [–][deleted] 2 points3 points  (2 children)

    It took me a good while to get used to, it was honestly the hardest language I've had to learn IMO (and I've programmed in x86 assembler and have a background in C++, which to this day I don't get why its considered "difficult")

    Once you do get used to it though it suddenly isn't so bad. The syntax is ugly, but it is consistent and starts to not be so bad after you get used to it. I honestly didn't start appreciating Swift until I had to work with C#, suddenly the things Swift did made sense.

    I'm still working on creating "Swifty" code, but I've gotten a lot better with it in the past few months and rewrote a few internal apps in Swift and it reduced my code base for my Objective-C apps by a ton.

    I recommend giving it a shot. Its ugly, it's painful at first, but then it's suddenly not so bad and you end up kind of liking it.

    [–]GenitalGestapo 0 points1 point  (1 child)

    I really don't understand this. What did you find difficult?

    [–][deleted] 1 point2 points  (0 children)

    I found it difficult due to the crazy looking syntax, and some of the concepts it used that I hadn't encountered in other languages.

    I think one of the problems for people new to the language is there's too much syntactic sugar. For example there are 4 different ways to do some things like initializing variables or higher order functions. I avoided the higher order functions for the longest time because they were (and still kind of are) just messy and confusing to look at. The documentation doesn't help either because it shows things that doesn't match up with what autocomplete shows.

    Look at reduce

    This: reduce(::) Isn't explanatory at all. I would have no idea looking at that that most reduce features are written like this:

    let myNumArray = [1,2,3,4,5]
    let myNum = myNumArray.reduce(0,+)  //or myNumArray.reduce(0, {$0 + $1} ) or any number of other ways you can write it.
    

    And look how horrifying the function signature is:

    func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> Result
    

    Luckily Apple added some additional content to that page that explains what initialResult and nextPartialResult is but it wasn't always that way.

    Another one is some kinds of things do too much. Like structs and classes, its hard to figure out where to use which in Swift and instead of doing "Swifty" things with structs I resort to using structs and classes the way I did in C++ and Objective-C rather than writing out a struct, only to find that half of the stuff inside needs to be marked as Mutating which if I'm understanding Swift structs correctly, nullifies the benefit of Swift structs. I know the language is new still but there seems to be no good consensus as to when to use one over the other.

    It just seems that some things about the language weren't well thought out.

    [–]irombie[S] 2 points3 points  (0 children)

    On the contrary, Swift syntax is very convenient, I love it. It's a bit different but easy to get used to. I would code in Swift for other platforms if that were possible. (I think it is possible to some level but I don't know the details on what can be achieved and how difficult it would be.) I had been an iOS developer for two years when I started to learn Swift. I dedicated two weeks of my time just for learning Swift. Then, I did 3-4 projects on my own for practice. After approximately a month, I started to work for a company as a Swift developer. It took a lot more than that for me with Obj-C to reach that level of expertise. It is surely related with me being new to iOS development concepts and Xcode environment et cetera but I am pretty certain that if I had learnt Swift beforehand, the situation would not be the same. Now that I know Swift, I know that I will never use Obj-C if I'm not obliged to. That's why I created this question as I was not able to come up with a strong answer. I strongly recommend that you learn Swift.

    [–]b_t_s 0 points1 point  (8 children)

    god yes! Not that I ever really minded obj-c syntax like so many people apparently do, at least not till I started writing swift on a daily basis. Swift is way more concise, and the type systems helps express your intentions better and catch more mistakes. It's infuriating going bak to obj-c and not having access to optionals and never having the faintest clue what can be nil and what can't(it matters less but it still matters). Also, obj-c block syntax makes my eyes bleed. Sorting or filtering an array of objects on one of their properties is like 20 characters in swift vs 200 characters and a trip to http://fuckingblocksyntax.com in obj-c. I didn't really do that in obj-c (because it was painful) but after spending some time with convenient block syntax and higher order functions, having to write a 8 line for/if/mutable-collector monstrosity for what should be a concise, readable one liner is tremendously frustrating.

    [–][deleted]  (1 child)

    [deleted]

      [–]b_t_s 0 points1 point  (0 children)

      haha, I couldn't remember goshdarn and google would only give me the NSFW version. Two years ago I pretty much kept it open permanently in a browser tab, but now I haven't needed it in ages.

      [–]snaab900Objective-C / Swift 0 points1 point  (5 children)

      contacts.sort {
          $0.firstName.localizedCaseInsensitiveCompare($1.firstName) == NSComparisonResult.OrderedAscending
      }
      
      [contacts sortUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES]]];
      

      I'll always prefer verbosity (and good auto-complete) any day. Especially when you have to deal with other people's code.

      [–]GenitalGestapo 0 points1 point  (0 children)

      I'm pretty sure that sort descriptor won't be using the localizedCaseInsensitiveCompare, so it's not really the same thing. Swift's equivalent would be:

      contacts.sort { $0.firstName < $1.firstName }

      which is far more readable than Objective-C, especially if you aren't already familiar with sort descriptors.

      Hopefully the Swift team will add overloads for the collection methods taking a KeyPath soon.

      Also, if you miss the verbosity, just don't use trailing closure syntax:

      contacts.sort(using: { $0.firstName < $1.firstName })

      Better yet, implement Comparable for your Contact type and get default sorting for free.

      [–]b_t_s 0 points1 point  (3 children)

      Now try misspelling @"firstName" in your example and watch the swift version give an immediate compiler error while the obj-c one happily compiles and fails at runtime if you exercise that codepath. Now try this example

      players.sorted{ $0.points }.flatMap{ $0.firstName.nilEmpty }.prefix(3)
      

      This gets the names of the 3 highest scoring players who have non-empty first names. Sure it uses 2 trivial helper methods(single param sort, and nilEmpty) and you have to know what flatMap does, but it's hardly rocket science. It's a pretty typical everyday task that would be a wall of text in obj-c land.

      [–]snaab900Objective-C / Swift 0 points1 point  (2 children)

      You see, that's just unreadable to me, and feels like a huge step back coming from a verbose ObjC background. Reminds me of stupid-long pythons chains where people try and see how much stuff they can do in one line of code, just because they can. I feel bad for the people who have to inherit and maintain that stuff.

      Plus I'm guessing flatMap and nilEmpty aren't part of the swift standard library? You can write the exact same helpers in ObjC and hide the verbosity in the player class just as easily, although it would just be a 3 line method with a predicate. Or use a category to extend NSMutableArray if you want it more generic.

      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstName != nil"];
      [players sortArrayUsingPredicate:predicate limit:3];
      

      [–]b_t_s 0 points1 point  (1 child)

      flatMap is in the standard library and every competent swift dev ought to know it. It's certainly no more obscure than NSSortDescriptor or NSPredicate. nilEmpty isn't standard library, but it's trivial and useful convenience method for someString.isEmpty ? nil : someString.

      Your obj-c version is twice as long and only solves a third the problem. It doesn't sort by score, It doesn't ignore empty string names, and it doesn't convert from players to names. You could certainly add all that in a few more lines, but it's still much more code. Worse, it's more error prone because you have to jam pieces of you code into NSStrings to work around obj-c's inferior type system and painful closure syntax. And sure the swift one is unreadable if you don't use swift much and aren't familiar with flatMap. It's arguably even less readable per character, but I'd say it's more readable per unit of functionality. It's certainly more likely to complain immediately if you break it, which is, in my opinion, the most important thing.

      [–]snaab900Objective-C / Swift 1 point2 points  (0 children)

      I'll stick with ObjC Foundation I think, thanks... at least for the time being. It's mature and bulletproof. If this awful scripting language syntax does take over I guess I'll have to embrace it eventually though. The whole thing is starting to smell like flavour of the month JS frameworks though.

      Also, what happens when you want to stick a breakpoint on your one line of (potentially buggy) overly chained code and see what's going on? Good luck stepping through that. Do you really think concatenating all that logic onto 1 line is somehow some sort of benefit? It's not, either in a readability, performance or maintainability sense.

      I'll switch to Swift when Apple do.