Dear iOS and OS X application developers, now is the best time to switch your programming skills to the more user-friendly and comprehensive Swift. Programming languages don’t die easily, but the big push to replace them is not one of them. If you develop mobile applications and haven’t tried Swift yet, take note: Swift isn’t just a new programming language that’s hoping to replace Objective-C on the Mac, iPhone, iPad, Apple Watch, and other future devices. It’s also a new programming language that’s going to replace C for embedded programming on Apple platforms. Thanks to several key features, Swift is quickly becoming the programming language of choice for creating immersive, responsive, consumer-facing applications in the coming years. Apple seems to have big goals for Swift. The language has a lot of optimizations for compiler performance and language development needs, and Apple hints in Swift's documentation that the language is "designed to scale from 'hello, world' to full operating systems." While Apple hasn't yet made clear what the language's design goals are, the addition of Xcode 6, Playgrounds, and Swift are signs that Apple wants to make app development easier and integrate it with any other development toolchain. In today’s article, we will look at the top ten reasons to understand the specific benefits of choosing Swift as your preferred programming solution. 1. Swift code is easier to read Objective-C almost destroyed all the expectations and hopes for a programming language built on C. In order to distinguish its keywords and type settings from C, Objective-C introduced the @ symbol as a new keyword marker. Since Swift is not based on C, it can unify all keywords and remove the @ symbol in each Objective-C type or object-related keyword. Swift has completely discarded many of the legacy conventions of its predecessor. So no more needing to end every line of code with a semicolon or to use parentheses to enclose conditional expressions in if/else statements. Another important change is that method calls in Swift no longer nest within each other, freeing us from the dreaded bracket hell - goodbye, [[[ ]]]. Swift's method and function calls use the industry-standard practice of using comma-separated parameter lists within parentheses. The result is a more concise and expressive programming language with a simpler syntax. Swift's code is very close to the natural language of English, or closer than other mainstream modern programming languages. This readability makes it easier for programmers who originally used languages such as JavaScript, Java, Python, C#, and C++ to incorporate Swift into their tool chain - unlike the ugly duckling Objective-C that was so difficult to deal with. 2. Swift code is easy to maintain This inheritance is the main reason why Objective-C has been held back in its development - if C doesn't evolve, then Objective-C won't evolve either. C requires programmers to maintain two code files at the same time to improve build times and improve the performance of application creation, and this requirement has been inherited by Objective-C. Swift eliminates this dual file requirement. In Swift 1.2, Xcode and the LLVM compiler can automatically determine the dependencies and perform incremental builds. In this way, the task of separating the table of contents (i.e., header files) from the body (i.e., executable files) no longer exists. Swift integrates the header files (i.e., .h files) and executable files (i.e., .m files) of Objective-C into a single code file (i.e., .swift file). Objective-C's dual file system undoubtedly adds extra work to programmers - which makes it more difficult for programmers to complete development tasks from a big picture perspective. In Objective-C, we must manually synchronize method names and comments between the two files so that both use the same set of standard expressions, but unless the development team already has existing rules and code review mechanisms, this goal cannot be guaranteed. Xcode and the LLVM compiler work behind the scenes to reduce the actual workload of programmers. In Swift, programmers almost don't have to worry about the above tasks, so they can spend more energy and time creating application logic. Swift simplifies boilerplate work, improves the quality of code and comments, and brings more functional support capabilities. 3. Swift is more secure One of the interesting aspects of the Objective-C language is how it handles pointers, specifically nil (aka null) pointers. In Objective-C, if you try to call a method on a nil (aka uninitialized) pointer variable, nothing will happen. The expression or line of code becomes a no-op, which is not likely to cause unexpected crashes, but is a major source of bugs in applications. A no-op often results in unpredictable behavior, which is the number one enemy programmers face when trying to find and fix random crashes or unexpected hangs. The advent of reliable types makes the possible consequences of no-op values in Swift code very clear, which means that once you write bad code, it will directly cause a compiler error. Swift creates a short feedback loop and allows programmers to code according to their intentions. Related problems can be solved while the code is being written, which obviously greatly reduces the time and effort we spend on bug fixing work - especially bugs related to Objective-C pointer logic. Traditionally, in Objective-C, if a value is returned from a method, the programmer is responsible for documenting the behavior of the pointer return variable in the file (using comments and method naming conventions). In Swift, optional types and value types make it easy to know whether a value exists or is potentially optional (i.e., the value may exist or may be nil) from the method definition. To provide predictable behavior, Swift triggers a runtime crash if a nil optional variable is used. This crash leads to consistent behavior, which simplifies the bug fixing process because it forces the programmer to fix the problem immediately. Such Swift runtime crashes prevent the corresponding line of code from running after a nil optional variable is used. This means that the bug must be fixed as soon as possible or completely eliminated from Swift code. 4. Swift has unified features in memory management Swift is able to unify its language in a way that Objective-C cannot. Swift's support for Automatic Reference Counting (ARC) covers both procedural and object-oriented code paths. In Objective-C, ARC is only supported in Cocoa APIs and inside object-oriented code; it does not work in C code procedures or APIs such as Core Graphics. This means that programmers must take care of their own memory management when using the Core Graphics API and other low-level APIs provided by iOS. As a result, Swift completely eliminates the massive memory leaks that programmers often face in Objective-C. Programmers should not have to think about memory management for every digital object they create. Since ARC can handle all memory management during the compilation process, everyone's brainpower can focus on the core logic of the application and various new features. Because ARC in Swift works across procedural and object-oriented code, it no longer requires programmers to waste too much energy on context switching - we can even write code that directly touches the underlying API, thus solving the biggest problem facing the current version of Objective-C in one fell swoop. By solving the problem of automatic and high-performance memory management, Apple has proven that Swift can really help programmers be more productive. Another side effect is that neither Objective-C nor Swift are affected by the garbage collector mechanism used to clean up unused memory, as Java, Go, or C# are. This is very important for any programming language that will be used to respond to graphics and user input, especially on touch devices like iPhone, Apple Watch, or iPad (where lag in operation can be an unbearable experience and cause users to think that there is something wrong with the application). 5. Swift requires less code Swift is able to greatly reduce the amount of code required for repetitive statements and string operations. In Objective-C, the process of processing text strings is very cumbersome and requires a series of steps to combine two sets of information together. In contrast, Swift has many modern programming language features, such as the ability to directly combine two strings together through the "+" operator, which is not available in Objective-C. The ability to support this kind of character and string combination has become a must-have for any programming language that needs to display text content to users on the screen. The type system in Swift reduces the complexity of code statements because the compiler can directly identify these types. For example, Objective-C requires programmers to use special string markers (such as %s, %d, and %@) and provide a comma-delimited list of variables to replace these markers. Swift supports string interpolation, which saves programmers from memorizing these markers and allows them to directly insert variables into user-facing strings, such as labels or button titles. In contrast, the type inference system and string interpolation in Objective-C are often the cause of app crashes. In Objective-C, messing up the order of string tokens or using the wrong string token could crash the application. But now, Swift can liberate programmers from these cumbersome regulations and ensure that the translated code is more concise (thus reducing the chance of code errors) with its inline support for text strings and data operations. #p# 6. Swift code executes faster From a deeper perspective, abandoning the legacy features of the C language has greatly improved Swift in many aspects. The outstanding performance of Swift code in performance benchmarks also proves that Apple has indeed been effectively improving the speed of Swift when running application logic. According to results released by Primate Labs, the developers of the popular performance tool GeekBench, Swift's performance in December was very close to that of C++ in a test of compute-bound tasks using the Mandelbrot algorithm. In February 2015, Primate Labs found that Xcode 6.3 Beta had further improved Swift's performance in the GEMM algorithm, a memory-bound algorithm that performs sequential access to large arrays, by 1.4, and in the original FFT algorithm, a memory-bound algorithm that performs random access to large arrays, by 2.6 times. Swift’s adoption of best practices makes this improvement even more significant, with an 8.5x improvement in FFT performance (while C++ only achieved 1.1x). This improvement also means that C++ only scored 1.03x as much as Swift in the Mandelbrot algorithm. Swift language is almost comparable to C++ in performance in FFT and Mandelbrot algorithms. According to Primate Labs, the GEMM algorithm performance test results prove that the Swift compiler cannot implement code quantization like the C++ compiler - and this quantization method is a convenient way to quickly improve performance, and will be officially launched in Swift in the next version. 7. Reduce naming conflicts with open source projects One of the biggest problems with Objective-C code is the lack of formal support for namespaces, which is how C++ handles conflicts in code file names. When this happens in Objective-C, it manifests as a link error and the app won't run properly. A solution has been proposed, but it can lead to other potential problems. Typically, programmers use a two- or three-letter prefix to distinguish between Objective-C code that they've written - in other words, like @ing our code on Facebook. Swift provides implicit namespaces, allowing the same code file names to be used across projects without causing build failures or forcing programmers to use names like NNString (Next Step, the company Jobs founded after leaving Apple) or CGPoint (Core Graphics). Ultimately, this feature in Swift allows programmers to be more productive and means they no longer have to keep track of file names like they did with Objective-C. In Swift projects, you can see simple names like Array, Dictionary, and String that are the result of this, rather than being forced to use NSArray, NSDictionary, and NSString, which are artifacts of Objective-C's lack of namespaces. With Swift, namespaces exist based on the target to which the code file belongs. This means that programmers can use namespace identifiers to distinguish between classes or values. This change in Swift is very significant. It greatly simplifies the process of integrating open source projects, frameworks, and libraries into your own code. The namespace allows different software vendors to create the same code file names without worrying about naming conflicts when integrating them with open source projects. In short, now Facebook and Apple can use an object named FlyingCar.swift without any errors or build failures. 8. Swift supports dynamic libraries The biggest change in Swift—and one that doesn’t get enough attention—is the move from static libraries to dynamic libraries, which have been around for several major releases (iOS 7, iOS 8, and beyond). Dynamic libraries are executable pieces of code that can be plugged into apps. This means that Swift apps can now keep up with the latest versions of the language as it evolves. Developers need to submit both their app and its corresponding library, and both are digitally signed with a development certificate to ensure their integrity (sounds like the NSA is on to something again). This means that Swift can be updated even faster than iOS, which is an important requirement for modern programming languages. Any changes to the library can be included in the latest update package of the app in the App Store, and the whole process is very simple to implement. Until Swift and iOS 8, iOS did not support dynamic libraries - but on the desktop, Macs already support dynamic libraries. Dynamic libraries exist outside the application executable file, but are downloaded as a bundle when users get the application from the App Store. Because this part of the content is loaded into memory, it reduces the initial size of the application, and these external codes are only accessed when they are used. On the Apple Watch platform, the ability to defer loading in mobile or embedded applications can improve performance during user experience. This is one of the unique features of the iOS ecosystem, that is, the experience is more responsive and fast. Apple has always focused on allowing applications to load only the assets and resources they need during operation, and now compiled access code is also included in this category. This instant loading during operation can reduce the initial wait time of the application and ensure that it is not actually loaded until it is needed to be displayed on the screen. The emergence of dynamic library support makes Swift a programming language that can be changed and improved at an unprecedented speed. Users no longer have to wait for iOS version updates to immediately enjoy any performance improvements or reliability improvements that Apple adds to Swift. 9. Swift Playgrounds encourages programmers to adopt an interactive coding approach Swift's new Playgrounds are a boon for experienced developers. Inspired by former Apple employee Brett Victor, Playgrounds lets programmers test a new algorithm or graphics routine in real time -- say, five to twenty lines of code -- without having to build an entire iPhone app. Apple has also added inline code execution to Playgrounds, which helps programmers get feedback from the development environment while creating a code snippet or writing an algorithm. This feedback loop can greatly improve code writing efficiency, because from a traditional perspective, programmers need to use their thoughts to estimate the running effect of the current code - with Playgrounds, it is always done visually. Programming is an iterative process, and any mechanism that can reduce or supplement the creation process can make programmers more productive and free up their energy to deal with more important problems - rather than focusing on solving the annoying details of the adjustment work imposed on programmers by traditional compilers. Note: From my personal experience teaching new programmers, Playgrounds don’t really stand out as much to newbies as they do to experienced programmers. Simply showing a variable in Swift Playgrounds doesn’t help newbies understand whether the code needs a floating point variable or an integer variable—which is especially true if you’re building an app that remembers where you last viewed your Facebook newsfeed. The “why” question is answered in the actual running iPhone app—not in the snippet in Playgrounds. 10. Swift represents a future we can participate in and influence Objective-C will continue to exist for a long time, but it will not see any major changes, and this is entirely due to the introduction of Swift. Some features in Swift may be migrated to Objective-C, but Objective-C is born out of C language and its lineage is destined to absorb only a little fresh blood. Swift brings a direct path to the development community, allowing each of us to influence it and use the results to create applications, embedded systems (if Apple releases an embedded framework license and provides chip architecture licenses to third parties in the future), and devices like the Apple Watch. Apple is focused on providing the best consumer experience and is working on building features that really deserve serious attention. With the release of Swift 1.2 in Xcode 6.3, Apple has fixed thousands of known bugs collected by Apple bug reports. The technical team responsible for supporting the development and evolution of Swift is very concerned about the fate of the language, and they look forward to observing how Swift can achieve better support and help the development community create excellent applications and system results. Swift: A more approachable and full-featured language option The aforementioned changes allow Swift to take the lead over Objective-C, while completely getting rid of many of the lingering problems of the latter as a derivative language. Apple will not abandon Cocoa, because it is the core API and code base that creates the special user experience of Apple products. Instead, they will launch a full-featured validation mechanism and make it easy for Cocoa to interact with other new APIs that can support features such as Force Touch or Taptic Feedback (i.e. force sensing and force feedback). Many traditional settings in the past were designed to ensure the ease of use of compiler design solutions. Swift focuses on getting rid of the nerve-wracking dregs of traditional coding practices, thereby helping application developers get their work done more easily. As modern compiler solutions continue to improve, we will be able to express more information with less code. With Swift, programmers need to maintain only half as many code files, do not need to synchronize code manually, and are less likely to make punctuation errors - this will ensure that they spend more time writing truly meaningful and high-quality code. In Swift, code can now be self-documenting and provide more optional types: compile-time safety mechanisms return a value or no value, which is precisely the root cause of many problems such as asynchronous operations, network failures, invalid user input, or data validation errors. In Swift, ARC unifies C-style procedural code and object-oriented code using Apple's Cocoa framework. Developers will find that they need to write less code in Swift and can use modern language features to support them in making their code lines more reliable. Swift itself will continue to evolve while keeping the Apple ecosystem at the forefront of programming trends, thanks to the dynamic library support provided by iOS and Swift. Open source projects, third-party SDKs and frameworks that integrate with home automation, devices and social services will be easier to connect without the extra build time. Swift has almost comparable speed performance to C++ in some algorithm tests, and the latest release of Xcode 6.3 and Swift 1.2 marks that Apple will further promote this performance optimization. And now with Playgrounds, Swift can provide programmers with visual feedback in a whole new way, helping us develop algorithms using inline data visualization mechanisms. With shorter feedback loops and graphical descriptions, the iterative coding process is easier than ever before. In conclusion, Swift is an approachable and full-featured programming language that allows developers to build applications with it while also targeting new low-power embedded system platforms such as the Apple Watch that will appear in the coming years. |
<<: Deviceone: Standing at the crossroads of the mobile Internet era
>>: Exclusive interview with Qiniu President Lv Guihua: Let cloud services penetrate into life
Keywords of this article: Li Jiaqi , Double Eleve...
The framework is as follows: 1. What is the real ...
Since the news that people can wear short sleeves...
How much does it cost to attract investment throu...
Expert of this article: Huang Lu, Director of Hel...
McDonald's packaging carcinogens On August 10...
On March 10, 1988, the first test-tube baby was b...
recently Various rumors circulating on the Intern...
It's hard to imagine that a common sneeze can...
Product promotion is a difficult problem. It look...
Spenser: My Personal Branding Course for Earning ...
Nowadays, plastic has become an indispensable mat...
When you are making tea, if you find some tiny fl...
As the epidemic is at a critical stage, breeders ...