Have You Explored The New String API In Swift 4 Yet?
In 2017, Swift 4 was released as a part of Xcode 9 at WWDC. Although it’s final release is supposed to be scheduled in September, but developers have already laid their hands on the new app development language. The new version brings some extraordinary improvements and much-needed additions to the existing Swift 2 and 3.
Key Features of the programming language, include:
- New Codeable protocol that allows for easy serialization of data wrapped in structs.
- With the new “”” declaration, string literals can now break into multiple lines (three sets of quote marks) to open and close the multiline string.
- Strings received a major overhaul, making them collections of characters.
Overall, Swift 4 can be considered more of a refinement than a complete revamp of the language. One of those refinements is “String API”, the most used types in every programming language. In a layman’s language, a string can be defined as the data type that we, humans, understand better. In the previous versions, many times Swift with its cumbersome way of handling characters and substrings went too far in favoring correctness over ease of use.
How is Swift beneficial for enterprises and developers?
Before migrating to Swift 4, let us understand the true significance of the technology for enterprises and developers. The open source language is widely being adopted at a rapid pace. As a result, developers are now able to prototype as well as write iOS, OS X, and other Apple platform apps pretty quick with fewer bugs and crashes than ever before.
This is very important for enterprises, especially businesses that have made investments in Apple’s platforms to develop their own B2B / enterprise apps. Because keeping those apps up to date has never been easier. In addition to this, Swift is a kind of language that can be easily expanded to other platforms like Linux, something beyond what Apple just created.
Web apps are such an area that carries much potential in Swift. And being ported to Linux, the technology can run on low-cost, low-maintenance Linux servers especially the ones which are already the cornerstone of existing web APIs and services.
Bye, Bye, string.characters
Coming back to Strings in Swift 4, I personally feel that it is a very welcomed change. This change removes the need for a “characters” array on String. This means, you can now reverse them, loop over them character-by-character, map() and flatMap() them, and more. For example:
let Veda = "Rigveda :)" for ch in Veda { print(ch) } // Here, Veda is a string...
Here, you get not only logical iteration through String, but you get specific understanding for Sequence and Collection also:
Veda.count // 4 Veda.isEmpty // false Veda.dropFirst() // "igveda :)" String(Veda.reversed()) // ":) adevgi"
This update will not only make your code clumsier but even difficult to read. I have come across numerous IOS app developers who tend to add castings and initializing Strings with Substrings- which is completely counter-intuitive. Further, in my opinion, Apple still requires to see a String as a collection of characters or else the concept might end up being a classical “model VS reality” issue.
For better understanding, let’s take an example from the very talk in the WWDC 2017 Video of “What’s new in Swift”. We all know that a character in Swift is not a single “character” but a grapheme (combination of multiple symbols, i.e. “é” is equivalent to “e”+”´”). Now when you add elements to the string, the collection will certainly increase, isn’t it? However, that’s not true for Strings. “Check out the WWDC example“.
Even if three elements are added, the total count of characters will remain is one. Besides, it may quite interest you to know that since Swift 1.0, the API of Strings in Swift has been subject to change every single year. Therefore, all iOS app development companies do not require to philosophize on the nature of Strings.
Other Changes include:
- Introduced a “…” symbol that indicates a range of iteration from a given String index to the end of the String.
- A new triple quote syntax to write Strings with multiple lines directly.
Conclusion
Almost all Swift apps and scripts deal with strings, the refinements made will be quite beneficial in future. I think it’s a nice tradeoff between correctness and ease of use requiring professionals to make deliberate choices around copying. Last but certainly not the least, I hope as soon as they realize how different string is from rest of Ints, they will stop messing with it.
New NetWeaver Information at SAP.com
Very Helpfull