Swift vs. Objective-c: 10 Reasons to watch Swift in the future

Source: Internet
Author: User
Tags naming convention


It's time to develop apps for iOS and Mac OS x using the easy-to-use, comprehensive swif language.


While programming languages are not so easy to fade away, the development team that insists on the fading paradigm is doing so. If you are developing applications for mobile devices and you have not studied swift, then note that when Swift involves the development of applications for Macs, iphones, ipads, Apple watch and future devices, it will not only crowd out objective-c, It will also replace the C language of embedded development in the Apple platform.


Due to several key features, Swift has great potential in the coming years to be the real programming language for creating immersive, responsive, user-oriented applications.


Apple seems to have a bigger target on Swift. Its compiler performance and development language were optimized, and Apple hinted in Swift's documentation that Swift was designed to be a small (display) "Hello,world" that would be able to complete the entire operating system. Apple has not yet put the target of the language, Xcode6,playgrounds and Swift's launch, together to uncover Apple's intentions: simpler application development, easier-to-use development toolchain.


This is 10 reasons to use Swift to work from now on and walk in the forefront of the race.


1. Swift easy to read

As you can expect in a C-based language, OBJECTIVE-C has all the warts. To differentiate between the keyword and the type of C, OBJECTIVE-C uses the @ symbol to introduce a new keyword. Because Swift is not built on C, it agrees with all the keywords and removes a large number of @ symbols in front of the Objective-c type and object-related keywords.


Swift discards the legacy of the Convention. Thus you no longer need a semicolon for the end of the line, and brackets around the conditional expression in the If/else statement. Another big change is that method calls are no longer nested into brackets in the deep pits--Goodbye, [[[[]]]. Calls to methods and functions in Swift Use industry-standard parameter lists that are separated by commas within a pair of parentheses. The result is a cleaner, more expressive language with simplified syntax and syntax.


In addition to other contemporary popular programming languages, Swift is more of a natural English language. This readability is easy to be included in their toolchain by other developers from Javascript,java,python,c# and C + +-not unlike objective-c, a stupid yellow duckling.


2. Swift is easier to maintain

The legacy of history will make objective-c more and more regressive--C without evolution, the language cannot evolve. C requires the programmer to maintain two sets of code files to optimize the build time and the efficiency of creating an executable app that continues on the objective-c.


Swift lost the need for both documents. In Swift1.2, the Xcode and LLVM compilers can be automatically calculated and perform incremental builds. This separates the Content list (header file) from the content body (the implementation file). Swift merges the Objective-c header file (. h) and the implementation file (. m) into a code file (. Swift).


Objective-c's two file systems have the extra work imposed on programmers--and these work can distract programmers from the overall problem. In objective-c you have to manually synchronize the method names and comments between the files, sometimes in the hope of a good standard, but unless the rules of the team and the code review system are in place, this will not provide you with any guarantees.


Xcode and LLVM compilers can do some work behind the scenes to alleviate the workload of programmers. With Swift, programmers can spend less of their brain-memory work, allowing them to earn more time on the job of creating app logic. Swift has cut off the boilerplate work for our programmers and has improved the quality of the code, annotations, and features that we want to support.


3. Swift is more secure

One interesting aspect of objective-c is that pointers-especially the nil (null) pointers-are the way they are handled. In Objective-C, if you call a method with a pointer variable with a value of nil (uninitialized), nothing will happen. An expression or a line of operations becomes an empty operation (No-operation (No-op)), which makes it look like there is no upside, but it has become a huge source of bugs. No-op can lead to unpredictable behavior, which is the enemy that programmers face when trying to find and fix some random crash, or to stop abnormal behavior.


The optional type in swift code makes the possibility of a nil optional value very clear, which means it can generate a compiler error when you write down a bad piece of code. This creates a loop of short-range feedback that allows the programmer to write code with the target. The problem can be fixed when the code is written, which saves you the time and money you need to fix bugs that come from objective-c pointer logic.


In the Objective-c tradition, if a value is returned from a method, (using annotations and the method's naming convention) it is the programmer's responsibility to demonstrate that the pointer variable is returned. In Swift, the optional type and value type make the value in the method definition exist, Or it may be optional (that is, the value may or may be nil), and these questions are clearly clear.


To provide a prediction of behavior, Swift triggers a run-time crash when the nil optional value is used. The crash provides a consistent behavior that relieves the stress of fixing the bug because it explicitly forces the programmer to fix the problem. When the Swift runtime crashes, it stops at the line of code where the nil optional value is used. This means that bugs can be repaired earlier, and can be completely circumvented in swift code.


4. Swift's memory management is unified

Swift is unified in a way that objective-c never had before. Support for automatic reference counting (ARC) is done on the entire procedural and object-oriented code path. In. Objective-c. , ARC is supported in the Cocoa API and object-oriented code, but it does not support procedural C-language code and APIs such as the Core Graphics. This means that the processing of memory controls is the responsibility of the programmer when using the Core Graphics API and the underlying APIs on other IOS. A lot of memory overflow problems that programmers encounter on Objective-c are not possible on Swift.


Programmers should not consider memory issues for the number objects he or she creates. Because ARC handles all memory management transactions at compile time, memory management can now be used to focus on core application logic and new features. Because the ARC in Swift works in both procedural and object-oriented code, it eliminates the need for the programmer to have a psychological context switch, even if they are writing code to reach the underlying API--which in the current version of Objective-c is a real problem.


Automatic and high-performance memory management is a problem that has been solved, and Apple has proven that solving this problem can improve productivity. Another side effect is that objective-c and Swift do not encounter the garbage collector running cleanup jobs for memory that is not being used, as Java,go or C # does. This is a very important element for programming languages that will be used for the appropriate graphics and user input, especially on touchscreen devices such as the IPhone, Apple Watch, and the IPad (if the response lag will make the user aware that the application is bad).


5. Less Swift code

Swift reduces the amount of code required for repetitive statements and string operations. In Objective-c, it is tedious to combine two pieces of information with a text string. Swift uses the features of a contemporary programming language, such as adding two strings together using the "+" operator, which is not in objective-c. This supports a combination of characters and strings for any programming language infrastructure that wants to present text to the user on the screen.


The type system in swift reduces the complexity of code statements-as a type that the compiler can understand. For example, OBJECTIVE-C requires programmers to memorize special character tokens (%s,%d,%@) and provide a comma-delimited variable to replace each tag. Swift supports string insertions, which eliminates the need to remember tags and allows programmers to insert variables directly into user-oriented strings, such as labels or button titles. This type of inference system and string insertion reduce the source of the error in objective-c is common.


In Objective-c, messing up the order or using the wrong string tag can cause the app to crash. Here, Swift again frees you from the lock-in work, translates into less code to write (the code is now not prone to error) because of its inline support for handling text strings and data.


6. Swift Faster

Removing the remaining C-language conventions greatly improves the performance of Swift under the hood. Benchmarks for swift code performance have always been aimed at the speed with which Apple is working to run app logic with Swift.


According to a survey of the Primate Research Institute (Primate Lab), the creator of the popular Geekbench Performance tool, the performance of compute-intensive tasks using the Mandelbrot algorithm (Mandelbrot algorithm) in December 2014, Swift is already approaching the performance of C + +.


In February 2015, the Primate Research Institute found that the Xcode 6.3 Beta boosted Swift's performance on the GEMM algorithm-a memory-constrained algorithm that requires sequential access to large arrays-1.4 times times. The initial FFT implementation-a memory-constrained algorithm that randomly accesses large arrays-has a 2.6 times-fold performance boost.


By applying best practices, you can observe a further performance improvement, which results in 8.5 times times the performance of the FFT algorithm (1.1 times times the difference in C + +). These improvements have also enabled Swift to actually surpass C + + 1.03 times times in the Mandelbrot algorithm.


Swift is almost as close to C + + as the FFT and Mandelbrot algorithms. According to Primate Labs, the performance of the GEMM algorithm shows that the Swift compiler cannot implement vector code supported by the C + + compiler--so the next version of Swift may be easier to get a performance boost.


7. Fewer name collisions in open source projects

The problem with the OBJECTIVE-C code is that there is a lack of formal support for namespaces, which is a solution for C + + to handle file name collisions. When a name conflict occurs in Objective-c, it is a connector error that causes the app to not run. The solution is there, but they all have potential pitfalls. The general Convention is to use two to three letter prefixes to differentiate between written objective-c code, for example, through Facebook to show your own code.


Swift provides an implicit namespace that allows the same code files to exist in multiple projects without creating a build failure, or to NSString (a company created after the Next Step-Steve Jobs was fired from Apple) or Cgpoint (Core G Raphics) such a name. Ultimately, this feature in Swift makes developers more productive, and it also means that they don't have to do the objective-c memory work they need. In a name such as Array,dictionary and String, you can see the impact of Swift, not the Nsarray, Nsdictionary, and NSString in the objective-c that are missing the namespace.


The Swift namespace is based on the destination where a code file belongs. This means that you can use a namespace identity to distinguish between different classes and values. This change in Swift is great, and it greatly facilitates the integration of developer projects, frameworks, and libraries into your code. namespaces make it possible to integrate open source projects without worrying about conflicting code files from different software companies. Now Facebook and Apple can use an object code file called Flyingcar.swift, without any errors or failures.


8. Swift supports dynamic libraries

One of the biggest problems in Swift that has not received enough attention is the switchover of the static library to the dynamic library, which is updated in the main release (IOS8,IOS7, etc.). A dynamic library is an executable block of code that can be linked to an app. This feature allows existing swift applications to link to the updated version of the Swift language that has been generated over time.


Developers submit the app together with the library files, which are digitally signed with a developer's certificate to ensure integrity (Hello, NSA). This means Swift can evolve faster than IOS, which is necessary for a modern programming language. Changes to the library files can be all introduced into the latest update of an app on AppStore, which is so easy to run together.


The dynamic library has never been supported on IOS until Swift and iOS 8 are released, although it has been supported for a long time on the MAC. The dynamic library is in addition to the application executable file, but is included in the app package downloaded from AppStore. It reduces the initial size of the app being loaded into memory, because external code is only linked in when it is used.


Mobile apps or the lazy loading capabilities of embedded apps on Apple Watch will improve the perceived performance of the app for users. This is one of the differences that make the IOS ecosystem more sensory responsive. Apple originally focused only on loading data and resources at runtime, and now the code compiles and links can be run at runtime. The runtime loads the reduced wait time until the resource needs to be displayed on the screen before it is loaded.


Dynamic libraries in Swift have made it possible to upgrade programming languages faster than ever before. Users are not required to wait for the specified iOS release to enjoy the performance and reliability improvements Apple introduced in Swift.


9. Swift Playgrounds encourages interactive encoding

Swift's newly introduced Playgrounds is the Gospel of experienced developers. Playgrounds's inspiration came from the work of Apple's former employee, Brett Victor. Playgrounds allows programmers to test a new algorithm or graphics program with 5 to 20 lines of code, instead of creating a complete iPhone application.


Apple has added inline code execution to Playgrounds to help programmers create blocks of code or write some sort of algorithm to get feedback. Such feedback loops can increase the speed of code writing, because the mental models required by traditional programmers have been replaced by Playground data visualization forms. Programming is a recurring process, and any possible stress mitigation or creation supplement can make developers more productive and unleash their energy to solve bigger problems instead of staring at traditional compilers to add cumbersome details to programmers.


Note: According to my experience as a novice programmer, Playgrounds is not as useful to beginners as it is for an experienced programmer. If you simply show how a variable in Swift works, Playggrounds obviously can't help beginners understand the need for a floating-point pointer variable with an integer variable. This will only become apparent when you want to show a place where you can remember your last scrolling position in the Facebook News feed. For beginners, the "why" issue can only be answered with one running example: an IPhone app.


Swift is a future you can influence

Objective-c There is no way out, you will not see it change, we want to thank Swift for the introduction. Some of the swift features may be integrated into the objective-c, but Objective-c's C-language legacy is destined to absorb only that bit of Swift's good stuff.


Swift provides a direct way for the developer community to influence a language that will be used for the creation of applications, embedded systems (if Apple authorizes third-party embedded frameworks and chips), and devices like Apple Watch.


Apple is focused on delivering the best consumer experience and building only the features that are noteworthy. With the release of Swfit1.2 in Xcode6.3, Apple has fixed thousands of bugs with the popular Apple bug Reporter tool. The team that supports swfit development and improvement is interested in improving the language to better support the development community that uses Swift to build applications and systems.


Swift: Easier to learn and feature-rich language

Starting with the traditional language of discarding objective-c, a lot of changes have made Swift go beyond objective-c. Apple did not discard cocoa--. This is their API and code base for creating an apple-style experience-rather, it provides the equivalent of a full function, making it easier to interact with new APIs that support Force Touch or taptic Feedback features.


Many of the old design decisions are designed to make the compiler easier to design. Swift is focused on making it easier for application developers to work by abandoning traditional nervousness and coding practices. With the development of modern compilers, a small amount of code can represent more information.


With Swift, programmers maintain half of the original code files, manual code synchronization is zero, and punctuation input errors are much lower than before-so you can spare more time to write high-quality code. By using an optional type-a compile-time security mechanism that returns or does not return a value-the return value is a synchronous operation, a user input that is not valid when the network fails, and a common problem that occurs when a data validation error occurred. ARC is unified in Swift for procedural C-style code, as well as the object-oriented code used by Apple's COCOA framework.


Developers will find that they write less Swift, while modern programming language features support the readability of their lines of code. As it continues to evolve, Swift will maintain the advanced nature of the entire Apple ecosystem in programming, thanks to support for dynamic libraries in IOS and Swift. Open source projects, third-party SDKs, and frameworks can be more easily integrated into home automation devices and social services without the growth of compilation time. Swift is almost as fast in some algorithms as C + +, while the latest version of Xcode 6.3 and Swift 1.2 targets more performance optimizations at this starting point.


Plus playgrounds and swift allow a new way to develop visual feedback to assist in the use of inline data visualization algorithms to make a shorter feedback loop and a graphical description of the iterative decoding process easier to start.


Ultimately, Swift is an approachable, full-featured programming language that will allow developers to build apps and support embedded systems such as new low-power Apple Watch in the future.


English Source: PC Advisor

English Connection: http://www.pcadvisor.co.uk/news/software/3611295/swift-vs-objective-c-10-reasons-the-future-favors-swift/

Source: Oschina

Link: http://www.oschina.net/translate/swift-vs-objective-c-10-reasons-the-future-favors-swift

Swift vs. Objective-c: 10 Reasons to watch Swift in the future

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.