6 ms·
Has anyone used Swift yet? Report please?
by Kluny 12y ago
Has anyone used Swift yet? Report please?
- archagon 12y agoI've been using it for my latest project. Haven't done anything too fancy with it yet. Lack of automatic type conversion between ints and floats is driving me up the wall a little bit, but maybe that's a personal flaw rather than a flaw with the language. String manipulation is also incredibly confusing to me: I have yet to figure out how to get the number of characters in a string, or how to generate a random character. Other than that, it feels pretty nice to use. EDIT: also how to get character n in a string.
- josephlord 12y ago.utf16count will get you the number of characters in a simple string[0]. [0] What is a character especially in various non-latin scripts? I think it is well handled in Swift, from what I can see it has been considered carefully but I haven't yet needed to get my head round it. Edit: Corrected - too fast in the playground and actually checked an array. .utf16count not .count
- josephpmay 12y agoI believe Apple says to use countElements(string) instead of string.count
- archagon 12y agoYeah, I understand why they did it, but the fact of the matter is that I'm using ASCII 99% of the time.
- pjmlp 12y agoBut not the rest of the world.
- mythz 12y agoYou can add your own operator overloading of different number types to mitigate the casting required see: https://github.com/jquave/EasyCast https://github.com/jquave/EasyCast You can also add your own String extension for length and index character at in a String with: extension String { var length: Int { return countElements(self) } subscript (i: Int) -> String { return String(Array(self)[i]) } } Which will now let you use `.length` on strings, e.g: "Cat".length; //3 And get the character at index with: "Cat"[1]; //a I've more useful extensions at: https://github.com/mythz/swift-linq-examples/blob/master/src/LinqExamples/extensions.swift https://github.com/mythz/swift-linq-examples/blob/master/src... Which enables LINQ-like querying in Swift: https://github.com/mythz/swift-linq-examples https://github.com/mythz/swift-linq-examples
- TylerE 12y agoIn general, there are good reasons to not do implicit casts, especially between ints and floats. For one, it's actually a fairly expensive operation.
- JoshTheGeek 12y agocountElements(string) returns the number of Unicode code points in a string. I'm not sure about the random character, though. Swift is very much oriented towards Unicode rather than bytes when it comes to strings.
- josephpmay 12y agoTo get the number of characters in a string, call countElements(string). To generate a random character, your best bet (IMO, someone else may have a more elegant solution) is to have an array (or dictionary) or characters and use a random number generator to select a character from the structure.
- mikeash 12y agoIf you have a range of unicode code points that you want to select from, you can generate a random number in that range and then turn it into a one-character string like so: let codepoint: UInt32 = ...random choice code here... let scalar = UnicodeScalar(codepoint) let string = String(scalar)
- josephlord 12y agoIt would be easier to do this from an array of strings but I found an ugly inefficient way to do it from the characters in an arbitrary string: func randChar(alphabet:String?)->String { let alpha = alphabet ? alphabet! : "abcdefghijklmnopqrstuvwxyz" let rand:Int = Int(arc4random_uniform(UInt32(countElements(alpha)))) var gen = alpha.generate() for i in 0..<rand { gen.next() } return String(gen.next()!) } mikeash obviously has the answer if the codepoints are consecutive. This was quite annoying to do, more so than I expected. If you convert to UTF16 view you can alter the index by advanceBy but then I couldn't find an easy way to convert back from the UInt16 to a Character/String but mikeash also covers that. However I'm not sure that conversion to UTF16 is any less work internally than iterating through the index.
- micampe 12y agoHere is a great article about strings in Swift: http://oleb.net/blog/2014/07/swift-strings/ http://oleb.net/blog/2014/07/swift-strings/ Short story: “number of characters” and “nth character” are more complicated questions than you might think when you deal with full Unicode text.
- archagon 12y agoRight, but it's a problem when you're mostly dealing with ASCII identifiers and so forth. Looks like a great article, thanks!
- josephpmay 12y agoThere's been a lot of prior discussion about Swift on HN. I think the general consensus is that it's a fun and somewhat-well designed language that's buggy in places but will eventually make a good Objective-C replacement. I personally have enjoyed playing around with it. If Apple open-sources it, I could see using it as my main language (to replace Java, which I hate).
- dnissley 12y agoWhy wait? There's already a language that bears a strong resemblance to Swift and it even has Java interop baked in. That would be Scala.
- josephpmay 12y agoI want to get away from the JVM
- revscat 12y agoMind if I ask why?
- josephpmay 12y agoThese are probably not very good reasons, however: -It's slow -It's annoying for testing -It's run by Oracle -It feels antiquated
- curtis17 12y agoSame here. I want to get away from Java and the JVM. Twenty years old and now owned by Oracle.
- zmmmmm 12y agoIf you want a strong resemblance, Swift looks spookily like Groovy - to the point where I have to actually check sometimes that I'm not reading Groovy code. I would say much more so than Scala.
- joeblau 12y agoYes. I've built two apps on it. One is an Emoji Social network like Yo, but with Emojis, and the other is a Weather app that uses forecast.io. This is what I've been telling everyone. Swift does not make most of iOS App development easier; For the average person, the only benefit your'e going to get is no header files and 0%-30% shorter class files. The majority of iOS is about learning the libraries and API's. The main benefit of of Swift for the common user are that you can easily define and use your variables without giving them a type. There are also some niceties in Swifts functional programming style. As a developer on the iOS platform for the past 6 years, my problems were never with obj-c, they were with all of the "other stuff." If you don't understand how to layout an app and use a UITableViewController correctly, Swift doesn't help. If you don't understand the delegate callback or segue design patterns, Swift doesn't help. If you don't understand the PKI required to create your Dev and Prod certificates, Swift doesn't help. If you don't don't know to use Interface Builder to connect your UI's to your controllers, Swift doesn't help. If you don't know what keys to put in your info.plist to tweak the status bar or import certain settings, Swift doesn't help. Now from the advanced developer perspective, Swift is amazing. If you have a chance to watch Apple's advanced Swift talk (404 Advanced Swift), that's where you really see the power of the language come into it's own. You can literally re-write the language with the language. Mattt Thompson published a public gist[1] a few days ago where he showed how you can define mathematical constructs of unicode characters that aren't natively defined in the language. So for example, you could define the symbol square root. operator prefix √ {} @prefix func √ (value: Double) -> Double { return sqrt(value) } √25 // Outputs 5.0 Obviously, you'd have to do a bit more work on that to handle negative square roots and get into imaginary numbers, but things like this give you a glimpse into the power of the language. The real power of Swift is probably not going to be used by a lot of app Developers, but the potential of the language is a lot better than Objective-C ever had. Plus, you can now use emoji's as variables so that right there is worth learning it. [1] - https://gist.github.com/mattt/f457625af116721ffb57 https://gist.github.com/mattt/f457625af116721ffb57
- Xcelerate 12y ago> If you don't understand how to layout an app and use a UITableViewController correctly, Swift doesn't help. If you don't understand the delegate callback or segue design patterns, Swift doesn't help. If you don't understand the PKI required to create your Dev and Prod certificates, Swift doesn't help. If you don't don't know to use Interface Builder to connect your UI's to your controllers, Swift doesn't help. If you don't know what keys to put in your info.plist to tweak the status bar or import certain settings, Swift doesn't help. So I learned Swift in like a day, but all that stuff you listed I have absolutely no clue about. I just began iOS development and it's kind of overwhelming to open Xcode for the first time to see 57 different panels with 33 options in each panel (compared to opening Vim for instance haha). What would you recommend as a good resource for learning the "correct" way to use things like UITableViewController? Of course I can hack together an app that functions well enough but I'd like to use the API in the proper, idiomatic way if possible.