Android and Swift iOS development: language and framework comparison, androidios

Source: Internet
Author: User
Tags try catch

Android and Swift iOS development: language and framework comparison, androidios

Swift is the language that Apple currently promotes. It was launched in 2014 and is 10 years younger than Scala and other "new" languages. In the autumn of 2015, it was already open-source. Currently available on linux, Android NDK has been supported recently; on Raspberry Pi, there is a SwiftyGPIO library that can control some hardware through GPIO. Object C is old and uugly. oc was launched by Apple in 1983, which is too old and bloated.

The Swift syntax is similar to Scala, Javascript ES6, Java, OC, C ++, and Python.

This is the ranking of programming languages released by tibench.

Swift supports multi-paradigm programming: Protocol-oriented, object-oriented, and functional programming. Finally, we can also use Swift to learn functional programming ideas, which is supported by Java 8.

Java vs. Swift syntax comparison

Basic syntax

The switch Syntax of Swift is similar to that of Java and C ++, but it does not have a break. When it hits a case, it automatically exits the switch. When several different cases are the same, separate them with commas.

The for loop is basically the same as Java, but no parentheses are required. In the for loop, the usage of... <is more convenient. The underscore _ (replace the variable in the loop) can ignore the specific value and does not provide access to the value of the loop duration. For-in is a bit similar to for each loop in Java.

In Swift 2.2, try catch and do while differ greatly from java.

 

Functions and closures

The definition of the wift function is very different from that of Java. The definition of the Swift function is like func foo (arg: Type)-> Return Type:

  • Functions in Swift are first-class citizens and can be used as return values and parameters. Swift supports closures, and Java 8 supports lambda closures.

  • Swift supports tuples. Swift functions support multiple return values by returning tuples.

  • Swift functions can be nested, that is, a function can also be defined internally, which is not supported by Java.

  • Swift functions can receive variable parameters, which are similar to Java.

  • Swift function parameters can have default values. Similar to Python, Java functions cannot have default values.

The commonly used functional programming methods map, reduce, flatMap, filter, and sort are more important than understanding abstract functional programming concepts.

Struct vs. class

Struct is a value class, class is a reference type, Java language does not have struct, but c/c ++/c # language has, but cannot contain methods.

Struct rather than class is recommended for Swift development. The Swift language includes hundreds of struct and only a few classes.

The Swift class constructor is init (), The Destructor is deinit (), and the class method calls are basically the same as Java.

Self is equivalent to this in Java. When passing in a closure with inconsistent lifecycles, it must be declared as weak.

Enum Enumeration

Google does not recommend using Enum for Android development, which affects performance.

Swift's Enum is similar to Java and essentially a class that can contain functions.

The Swift Enum syntax is simpler.

Swift Enum supports extended extension.

Interface vs. Protocol

 

Extension is to add a new function (functionality) to an existing class, struct, or enumeration type ). This includes the ability to extend the type (that is, Reverse Modeling) without the permission to obtain the original source code ). The extension is similar to the category in Objective-C.

Extensions in Swift can:

  • Add computing attributes and computing static attributes

  • Define instance methods and types

  • Provides a new Constructor

  • Define subscript

  • Define and use new nested types

  • Make an existing type conform to an interface

The extension in Swift is very powerful. struct class enum can both be used or the system class can be extended. Note that the scope of the extension method is not discussed here.

MultiThread Multithreading

 

IOS multithreading is relatively simple compared to Java, and GCD can be understood in one day. Java's Concurrency package is complicated.

 

Android and iOS UI development comparison

IOS developers Apple officially recommends using Storyboard to develop the UI. The advantage is that it is more intuitive. You can better understand and maintain the App by looking at the interface. Now Xcode7 supports AutoLayout and SizeClasses better and better, and multi-resolution adaptation becomes easier. We recommend that you stop using frame code to write the interface.

Of course, Storyboard also has the following drawbacks:

The interface is mainly generated by IB, And the Xib code is difficult to maintain. If Xcode opens Storyboard or Xib, it will modify the file. Even if we do not make any actual modification, git will display the file modification.

Multi-person assistance, modification at the same time leads to conflicts and difficulty in merging.

When the Storyboard contains more pages, it will occupy too much memory, resulting in Xcode freezing and crash.

Difficult to locate errors, unclear error prompts, and difficult to locate Errors for new users. For example, it is difficult to locate IBOutlet accidentally deleted.

Storyboard UI development practices

The service modules are divided into multiple storyboards, and each module is responsible for avoiding overlap.

Each storyboard must have no more than 10 pages. You can use the Refactor Storyboard function to redivide them.

Use Container to reuse the UI module in one storyboard and xib to reuse the UI module in multiple storyboard.

For complex input forms, we recommend that you use the SwiftyForm framework to write code.

Using Size Classes for Multi-Resolution adaptation is easier.

 

Comparison between Android and Swift iOS frameworks

There are already many Swift frameworks. Swift can also use the OC open-source framework, but it is not recommended. We mainly compare and introduce the common network request frameworks, JSON parsing and image cache frameworks of the project.

Currently, network frameworks such as OKHTTP, Retrofit, and Volley are generally used for Android development. In the iOS development oc era, AFNetworking libraries are used for development. Alamofire and Moya libraries are recommended for swift development.

Moya encapsulates the Alamofire network request library and does not need to write network models or manage the library. Make the code more concise. Moya can replace the self-compiled network abstraction layer APIManager. Moya provides some good features:

  • Check API call correctness during compilation

  • Use the enum Enumeration type to clearly define APIs for different APIs

  • Taking Interface Test stub as a first-class citizen makes unit test very simple.

  • Supports ReactiveX extension to facilitate integration with RxSwift.

  • Moya is more capable of simulating data debugging than Android Retrofit.

Currently, most apps use JSON as the Message Protocol. for Android development, we generally use Gson for parsing. In Swift development, after comparing ObjectMapper, Argo + Curry, and SwiftyJson, we decided to use ObjectMapper as the JSON parsing framework. ObjectMapper supports the following features:

  • Converts an object to JSON and a JSON object to a class object.

  • Nested objects (single object, Object List set, and dictionary) are supported)

  • Supports custom conversion functions

  • Supports struct

  • Supports Realm and Alamofire integration, AlamofireObjectMapper

Realm is a popular database developed for iOS for mobile terminals. It replaces sqlite with Android.

Example:

Let user = Mapper <User> (). map (JSONString)
Let JSONString = Mapper (). toJSONString (user, prettyPrint: true)

Realm is a popular database developed for iOS for mobile terminals. It replaces sqlite with Android.

Image cache framework. Glide and Fresco are commonly used for Android development. SDWebImage is generally used for OC development. HanekeSwift is recommended for Swift development.

Comparison of development tools

Currently, Android Studio is the mainstream development tool, and Eclipse ADT was used before 2014.
Xcode is always used for iOS development. For Java/Android Developers, it is difficult to get started with Xcode. In particular, the Interface Builder, Xib, and view code directly generate event methods through connections, which challenges development habits and difficulties in locating problems. Android Developers are used to writing xml interface code.

Compared with Android Studio, Xcode is fast but not stable enough. It is normal to crash several times a day.
In terms of simulators, the Android Emulator is a virtual machine, and the startup and installation speed is relatively slow. The iOS is a Simulator, which is fast, but some functions cannot be simulated. We recommend that you use a real machine for development.

Package Manager & build tool comparison

In the early days of Android development, Ant was used to handle some tasks. Later, some teams used Maven to learn from the Java EE project. After Android Studio appeared, Google recommended Gradle. Android build tools are much more powerful than iOS.

CocoaPods was used in early iOS development. Now Carthage is recommended. In the future, Apple will promote the official Swift Package Manager after official Swift 3.0 release.

The advantage of Carthage is its conciseness. The disadvantage is that some frameworks are not supported yet, especially some open-source libraries of BAT in China.

Finally, we will introduce the Xcode Package Manager Alcatraz, which mainly provides Xcode plug-ins, templates and color modes. Compared with Android Studio, there are currently few plug-ins.

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.