From java/android to Swift iOS development: a comparison of languages and frameworks

Source: Internet
Author: User
Tags gcd

This article is my "mobile front" group April 28, 2016 share summary collation.

I've been developing Android since 2009 and started contacting Swift at the end of 2014, when the group once did a demo App and felt that the technology was not mature enough to be officially released. At the beginning of 2016, we formally used Swift to develop the Sales assistant app products, accumulated a rich experience in swift Project combat, the open source framework is the SWIFT version, the use of a lot of protocol-oriented and functional programming. IOS app developers are basically the previous Android version of the developer, while maintaining Android and iOS 2 versions, high efficiency.

Today's shared theme is how Java/android developers can quickly get started with Swift iOS development. The main contents are as follows:

1. Whyfrom java/android to Swift

2. Why is swift language worth learning?

3. Javavs Swift Syntax comparison

4. Android and iOS UI development comparison

5. Androidvs Swift iOS Framework Comparison

6. Comparison of development tools

1. Why Fromjava/android to Swift

Cost and efficiency, due to the current functionality of the Android and iOS app products, a team development can reduce the cost of business synchronization and communication, and avoid product functional differences and communication costs for Android and iOS two teams.

Swift syntax is closer to Java, relative to objective C easy to get started, I had an iOS project in 2012, learned OC, because can not tolerate OC strange grammar finally gave up, but this learning swift iOS process is smooth, the main threshold of learning is Xcode IB's use.

Whynot React Native?

Reactnative We also want to use the project. But the learning curve of Rn thought and grammar is a bit steep, and many people with web development experience don't necessarily like it. In addition many Android developers did not learn JS, learning react native cost will be higher. Learning react native Ultimately, it is necessary to have a certain understanding of the native development. Finally, in some functionally complex app,react native user experience is less than native.

Whynot HTML5 Hybrid App?

The user experience is not good enough for engineers who are unfamiliar with web development and have higher learning costs.

2014 we used the Ionic framework to make a hybrid app that works on iOS, but compares cards on Android's low-end machine, impacting the user experience.

2. Why is swift language worth learning?

Swift is now the language of Apple's main push, and the new language in 2014 is 10 years younger than "new" languages like Scala. 2015 autumn has been open source. Currently available on Linux, the Android NDK has recently been supported, and there are swiftygpio libraries on the Raspberry Pi that can control some hardware with Gpio.

OBJECTC was old and Ugly,oc was launched by Apple in 1983, too stale and bloated.

Swift syntax is similar to Scala,javascript ES6, Java,oc, C + +, Python, which I sort by syntax similarity.

Swift supports multi-paradigm programming: protocol-oriented, object-oriented, and functional programming.

Finally, we can also learn functional programming ideas through SWIFT, which is supported by JAVA8.

3. Comparison of Java vs. Swift syntax

L Basic Grammar

L function

l struct and class

L Enum

L Interface Vsprotocol

L multithread

3.1 Basic syntax

Java

Swift

Static final constant

The Java language does not, the Guava library provides

Java Support Self-increment + +, auto minus-

Logical control and C are basically consistent

Java has the Main method

Java required; line terminator

var variable, let constant

Selectable optional, via if Let pack

Swift is not recommended, and version 3.0 will not support self-increment

Logical control statements are more modern

Swift has no Main method

Swift does not require line terminator

Swift's switch syntax is similar to Java and C + +, but it does not break and will automatically exit switch when he hits a case. For several different cases of the same case, you can use a few consecutive condition, separated by commas.

The For loop and Java are basically the same, but no parentheses are required. For loop, the. < is more convenient to use. The underscore symbol _ (the variable in the substitution loop) can ignore the specific value and does not provide access to the value when the loop is traversed. For-in is somewhat similar to the in-Java for Each loop.

Trycatch and do and Java differ greatly in Swift 2.2

3.2 Functions and closures

The definition of swift function is very different from Java, and the SWIFT function is defined as Func foo (arg:type)->return Type:

The function in Swift is a one-class citizen and can be used as a return value and parameter; Swift supports closures, and JAVA8 supports lambda closures.

Swift supports tuples, and swift functions can support multiple return values through the return tuple.

Swift functions can be nested, meaning that functions can also be defined inside a function, which Java does not support.

Swift functions can receive indeterminate parameters, basically similar to Java

Swift function parameters can have default values, similar to Python, where Java functions are not available with default values.

Commonly used functional programming methods Map,reduce, Flatmap,filter,sort, compared to understanding the abstract functional programming concept, I think it is more important to start with these functions first.

3.3struct vs. class

A struct is a value class, class is a reference type, and the Java language does not have a struct, but the c/c++/c# language has it.

Swift Development recommends using a struct instead of a class. The swift language implementation consists of hundreds of structs, with only a few class.

The Swift class construction method is init (), the destructor is Deinit (), and the class method call is basically the same as in Java.

Self is equivalent to this in Java, which needs to be declared as weak when it passes through a closure with a life-cycle inconsistency.

3.4Enum Enumeration

Android development Google official does not recommend the use of enum, affecting performance.

Swift's enum, like Java, is essentially a class that can contain functions.

Swiftenum syntax is much simpler.

Swiftenum supports extended extension.

3.5Interface vs. Protocol

Java Interface

Swift Protocol

? can inherit

? Interface cannot be implemented in a way that JAVA8 can

? Implements

? can inherit

? Protocol can be implemented with a specific method

? Extension more powerful

The extension extension is the addition of new functionality (functionality) to an existing class, struct, or enumeration type. This includes the ability to extend a type without having permission to get the original source code (that is, reverse modeling). Extensions are similar to classifications in Objective-c (categories).

The extensions in Swift can:

L Add computed properties and calculate static properties

L Define instance methods and type methods

L Provide new constructors

L Define Subscript

L Define and use new nested types

L make an existing type conform to an interface

It is important to note that the scope of the extension method is not discussed here.

3.6 multithread Multithreading

Java Android

Swift IOS

? Thread

? Java 5 Concurrency Package

? Android multithreaded Extensions

? Asynctask,looper ...

? Nsthread

? GCD

? Nsoperation,nsoperationqueue

The multithreading of iOS is relatively simple compared to Java, and GCD can be understood in a day's time. Java's concurrency package is a bit more complicated.

4. Android,ios UI Development Comparison

Java Android

Swift IOS

? XML layout Layouts

? Direct writing of XML code, Visual assisted development

? Android can add and implement view in Java

? Intent page Jump

? Java custom view, can be used in activity and layout, visual support is not good enough

? Xib & StoryBoard

? AutoLayout and Sizeclass,ib automatic generation, code merging difficult;

? Many teams have to implement the UI interface with code

? Segue Connection Jump

? Custom view can use Ibdesignable & runtime attributes

iOS Development Apple officially recommends using storyboard to develop the UI, the benefits are more intuitive, through the interface to better understand and maintain the app. Now that the Xcode7 version is getting better with AutoLayout and sizeclasses, the multi-resolution adaptation is simple, and it is recommended that you discard the traditional approach of using frame code writing interfaces.

Of course, storyboard also has the following drawbacks:

Interface mainly relies on IB generation, Xib code is difficult to maintain, Xcode open storyboard or xib will make changes to the file, even if we do not make actual changes, Git will show the file changes.

Multi-person assistance, while modification leads to conflict, merging difficulties.

Storyboard contains more pages and takes up too much memory, causing Xcode to stutter and crash.

Error location difficult, error is not clear, novice difficult to locate the error. For example, accidentally deleted Iboutlet will be difficult to locate.

Storyboard UI Development Practices

According to the business module divided into multiple storyboard, each module responsible for avoiding cross.

Each storyboard does not exceed 10 pages and can be re-divided by refactor storyboard function.

Use container in a Storyboard reusable UI module, with Xib in multiple Storyboard multiplexing UI modules.

Complex input forms, it is recommended to write code with the Swiftyform framework.

5. Android vs Swift iOS Framework comparison

The SWIFT framework is now a lot, and Swift can also use the OC Open source framework, but it is not recommended. We mainly compare the network request framework commonly used in the project, JSON parsing and picture caching framework

Network Request Framework

Java Android /p>

Swift IOS

   ;     Retrofit

?       OKHTTP

?        volley

?        

?        Retrofit + rxandroid

 

?       moya   

?       alamofire

?        afnetworking

?        

?        Moya + RxSwift

                                                                                               

open discover , retrofit and volley frame enter line open hair ,ios open ERA uses afnetworking library development, and Swift Development recommends using the Alamofire and Moya libraries.

moya For alamofire net ask library in Okay, package, open hair No need to write the net model, management, etc. Make generation code more concise. Moya can replace the network abstraction layer Apimanager that you write. Moya provides some good features:

compile time Check API interface Call correctness

Clearly defined interfaces for different APIs through enum enum types

Having the interface test stub as a class citizen makes unit testing simple.

support Reactivex extension for easy and rxswift integration.

currently the app is basically using JSON as the message protocol, Android development we generally use Gson to parse, in the swift development, compared with Objectmapper,argo+curry,swiftyjson, We decided to use Objectmapper as the JSON parsing framework. The features supported by Objectmapper are as follows:

supports converting objects to JSON, converting JSON into class objects

Support for nested objects (single object, Object list collection, and dictionary)

Support for custom conversion functions

Support struct struct

Support Realm and Alamofire integration, Alamofireobjectmapper
Examples of Use:

Let User = Mapper < User > (). Map (jsonstring)

let = Mapper ().    true)

Realm is a popular iOS-developed database for mobile-based design, instead of SQLite and Android version

Picture caching framework, Android development commonly used glide and FRESCO,OC development is generally recommended with sdwebimage,swift development hanekeswift.

6. Comparison of development tools

Android's current mainstream development tool is Android studio,2014 years ago as Eclipse ADT.

iOS development always uses Xcode. For java/android developers, Xcode is difficult to get started with, especially when interface Builder,xib and view code generate event methods directly through wires, challenging development habits, and having trouble locating problems. And Android developers are accustomed to handwritten XML interface code.

Compared to Android studio, Xcode is fast, but not stable enough, and it's normal to crash a few times a day.

On the emulator side, Android emulator is a virtual machine, startup and installation speed is slow, iOS is simulator, fast, but some features can not be simulated. It is recommended to use a real machine for development.

Package Manager & Build Tools Comparison

Android development early with Ant to do some task processing, some of the team behind the use of Java EE project with Maven,androidstudio appeared after Google recommended with Gradle. Android's build tools are much more powerful than iOS features.

iOS development early with Cocoapods, now recommended with Carthage, the future Apple official launch Swift3.0 will promote the official Swift package Manager.

Finally introduce the next Xcode Package Manager Alcatraz, mainly provide Xcode plug-ins, templates and color mode, compared to Android Studio, the current plug-in is still relatively small.


From java/android to Swift iOS development: a comparison of languages and frameworks

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.