A simple comparison between Swift and Objective C

  • 2020-05-15 02:14:42
  • OfStack

Now the competition between Swift and Objective C is growing fast. This is easy to understand, because they all have their own benefits, and some developers are confused about how to choose the right programming language for their project.

First, there is no strict answer to the choice between the two. There are many things to consider before making a choice, including various factors and characteristics, their respective disadvantages and advantages. So here's an overview to objectively show the differences and pros and cons between the two sides, because we think it's wise to choose based on whether it's a good fit for your development team and your specific project.

Management to consider

The first consideration is team specific selection. Even though Swift is often referred to as a simpler, smoother grammar language, it eliminates some of the more complex ones of Objective-C, there are still some barriers to entry. The right question here is not "what if the team could use Swift/Objective-C to build a new project", but "can the current team use it to meet deadlines?

Meet with your team and ask them, and make sure they can not only use the Swift implementation, but also get it done on time. If the team is an Objective-C expert, it is not a good idea to force them to use Swift and use it on a project with a strict deadline. And vice versa. Strict deadlines determine which language the team is most comfortable with (if not overwritten by the environment).

Project size

The next consideration might be the size of the project. Small projects may benefit from Swift's simple syntax, file structure, and other features, while large projects may be influenced by Swift, a young language. Of course there will be means of converting Xcode to new versions, but they are not perfect and things get worse as the project grows. Because of its youth, Swift also has some disadvantages, such as errors and instability. Taking Swift's native IDE-Xcode as an example, frequent crashes have been reported. Many incompatibilities of Xcode language have also been reported. But some new mistakes will come.

Technical reasons

There are many differences between the two languages, but they have at least one important feature - they are both apple's primary programming languages and are applicable to application development across all apple platforms. Objective-C was developed as early as the early 810s and belongs to Generation X. The original Cocoa was written in it, although the written part of Swift has been added to Cocoa and Cocoa Touch since its introduction in 2014.

Objective-C is a strict superset of C, and its object-oriented syntax is derived from Xerox Smalltalk. It is highly dynamic in nature, with dynamic bindings and dynamic typing, while Swift has static type checking.

Now let's look at the advantages of these two languages. One side advantage can be regarded as the other 11 side disadvantage.

Why Swift

Easy to use

The Swift design is easier to use and easier to read and understand than the Objective-C. Its syntax is closer to natural English, abandoning some traditional conventions - no more curly braces, no more "@" before any object related keyword or Objective-C specific type. It looks clearer; Swift is sometimes referred to as "Objective-C without C". Swift is not based on C, which not only simplifies the syntax, but also lowers the threshold for programmers to master Swift such as Java, Python, C ++.

Swift is also less verbose, especially in string manipulation, and even supports adding strings using a simple "+".

Memory management is common throughout the code, and unlike previous object-oriented approaches, automatic reference counting (ARC) can now be used in both object-oriented and procedural parts of the code. Objective-C has a huge memory leak, and Swift makes this impossible and frees up programmer development time.

In addition, Swift does not use a dual file system, there are no more.h header +.m implementation files, only one.swift code file (thanks to the LLVM compiler and Xcode IDE dependent handling).

security

Swift is designed to be a secure language. There is no fail-safe language in the world, but error handling is a good consideration and the type system is very powerful.

Unlike Objective-C, which doesn't throw an exception on nil pointer calls, for example, if you try to call a method on an uninitialized pointer variable, it just makes the expression operationless, the optional types and value types on Swift cause compiler errors to be generated on nil pointer calls. This means that the unpredictable instability associated with no operation disappears, and it greatly reduces error repair costs.

The dynamic library

In iOS 8, a long-awaited new technology has been brought to the mobile platform - support for dynamic libraries. Since then, the Swift application has been able to link to newer versions of Swift, allowing Swift to evolve faster than iOS, a requirement of modern languages. Dynamic libraries are also external application executables that allow for smaller initial downloads and differentiated workloads for mobile and embedded applications.

In the future

Swift may only be two years old, but since 2014, its popularity has grown rapidly to about 30% of all open source Cocoa projects. With apple clearly targeting Swift to replace Objective-C as their primary language, the future will shift to Swift.

This transition will take some time, but it is expected to take about 5-10 years, so there will be time for developers to get used to it.

Apple's favorite productivity software bug reporting tool shows how hard apple has worked to promote the Swift. Apple will also adapt to the community's ideas, absorb its knowledge and experience, and implement its solutions in future versions. The perfect way.

Why Objective-C

Compatibility and support

Despite the efforts of the Xcode team, IDE does not support Swift very well because it may be too new. Auto-complete lags, syntax highlights almost non-basic functionality, and refactoring tools don't work. Programming with Objective-C has been around for a while ( > It's been 30 years! , it is very mature and has very brilliant tool support.

Since Swift is "no C language", the C ++ library is not available here. The same includes C ++ cross-platform SDK and API basics (e.g. CoreFoundation).

There are some tools such as bridging and wrapping classes, but these efforts are usually not worth it. Stick with Objective-C and C ++ and Foundation API at best.

Swift is not only young, but also incompatible with older operating systems. Its support starts with iOS 7.0+ and Mac OS 10.9+ (tvOS and watchOS support any version of Swift), so Objective-C is the preferred tool platform for older platforms.

professional

Not only do some teams have more experience with Objective-C, but if you're interested in outsourcing part 1 of your project, it's easier to find third party experts.

dynamic

Although Swift can benefit from optimization from static type checking, the just-in-time method is no longer available and all types are defined before runtime. If you absolutely need dynamic typing, Swift won't do it.

run

Objective-C is a more deterministic platform, so it has a more stable, robust, and reliable runtime. We don't want to say that Swift can't achieve this scale, but it will take time.

conclusion

As we've seen, both languages have their own advantages and trade-offs, but they do have the potential to work together (on smaller projects, but preferably).


Related articles: