The guest is introduced
Zhang Xitao, Architect, full stack Engineer. 9 years of working experience in IT industry, familiar with Java, Android, JavaScript, Python, Swift and other technologies, started to pay attention to Swift iOS development in 2014, and recently focused on Swift. Currently, I am working as the mobile terminal architect of Km network (professional e-commerce SaaS platform). The tech blog is http://offbye.com.
I started Android development in 2009, and got to know Swift at the end of 2014. At that time, the team had made a Demo App, but I felt that the technology was not mature enough and had not been officially released. At the beginning of 2016, we officially developed and launched the sales assistant App using SWIFT, and accumulated rich practical experience in SWIFT project. All open source frameworks are swift versions, and protocol-oriented and functional programming are extensively used. IOS App developers are basically developers of previous Android versions, and maintain android and iOS versions at the same time, which is very efficient. Today’s topic is how Java/Android developers can quickly get started with Swift iOS development. The main contents are as follows:
-
Why From Java/Android to Swift
-
Why is Swift worth learning?
-
Java vs Swift syntax comparison
-
Android and iOS UI development comparison
-
Android vs Swift iOS framework comparison
-
Comparison of development tools
Why From Java/Android to Swift
In terms of cost and efficiency, as the functions of Android and iOS App products are consistent with those of the business, the development by one team can reduce the cost of business synchronization and communication, and avoid the product function differences and communication costs brought by Android and iOS teams.
Swift syntax is closer to Java and easier to learn than Objective C. In 2012, I led an iOS project and learned OC, but I gave up because I could not stand the strange syntax of OC. However, the learning process of Swift iOS was smooth this time, and the main threshold of learning was the use of Xcode IB.
Why not React Native?
React Native is also being used by the project. But RN’s ideas and syntax have a steep learning curve that many people with Web development experience don’t necessarily like. In addition, many Android developers have not learned JS, so learning React Native will be expensive. Learning React Native ultimately requires an understanding of Native development. Finally, React Native doesn’t offer the same user experience as Native apps with complex functions.
Why not HTML5 Hybrid Apps?
The user experience is not good enough, and the learning cost is high for engineers unfamiliar with Web development.
We have used ionic to make a hybrid App before, which can work well on iOS, but android low-end phones are stuck, which affects the experience.
We also have an App made by React Native, which was launched in November last year. The user experience is good, but the developers are mostly front-end developers.
Why is Swift worth learning?
Swift, now Apple’s main language, was introduced in 2014 and is 10 years younger than “new” languages like Scala. Open source in the fall of 2015. Currently available on Linux, Android NDK has recently been supported; There is a SwiftyGPIO library on raspberry PI that allows you to control some hardware through GPIO. Object C is old and ugly, OC was introduced by Apple in 1983, which was too old and bloated.
Swift syntax is similar to Scala, Javascript ES6, Java, OC, C++, Python, which I sorted by syntax similarity.
(Swift is on both Redmonk and TIOBE’s language lists.)
Swift is years younger than languages like Scala and Clojure.
Swift supports multi-paradigm programming: protocol oriented, object oriented, and functional programming. Finally, we can learn the idea of functional programming via Swift, which Java8 only supports.
Java vs. Swift syntax comparison
Basic grammar
Swift’s switch syntax is similar to Java and C++, but it has no break. It automatically exits the switch after hitting a case. If several different cases are processed in the same way, the case can be followed by several conditions separated by commas.
The for loop is basically the same as Java, but without parentheses. In the for loop,.. The use of < is convenient. The underscore symbol _ (instead of a variable in a loop) can ignore a specific value and does not provide loop traversal access to the value. For-in is somewhat similar to the For each loop in Java. <=”” p=””>
Try catch and do while in Swift 2.2 are very different from Java.
Functions and closures
Func foo(arg: Type) -> Return Type:
-
Functions in Swift are first-class citizens and can be returned as values and parameters; Swift supports closures, while Java8 supports lambda closures.
-
Swift supports tuples, and Swift functions can support multiple return values by returning tuples.
-
Swift functions can be nested, that is, functions can be defined within a function, which Java does not support.
-
Swift functions can accept indefinite parameters, similar to Java.
-
Swift function arguments can have default values. Similar to Python, Java functions cannot have default values.
Common functional programming methods map,reduce, flatMap,filter,sort. I think it is more important to start with these functions well than to understand 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 # languages do, but can not have methods.
Swift development recommends using structs instead of classes. The Swift language implementation consists of several hundred structs 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 the equivalent of this in Java and is declared weak when passing in a closure with an inconsistent lifecycle.
Enum enumeration
Android development Google official does not recommend using Enum, affecting performance.
The Swift Enum is similar to Java in that it is essentially a class that can contain functions.
Swift Enum syntax is simpler.
Swift Enum Extension extension is supported.
Interface vs. Protocol
Extension is adding new functionality to an existing class, structure, or enumerated type. This includes the ability to extend types without access to the original source code (that is, reverse modeling). The extension is similar to categories in Objective-C.
Extensions in Swift can:
-
Add computed properties and computed static properties
-
Define instance methods and type methods
-
Provides a new constructor
-
Define the subscript
-
Define and use new nested types
-
Makes an existing type conform to an interface
The extension in Swift is very powerful, struct class enum can be, can also extend the system class. It is important to note that the scope of extension methods is not discussed here.
MultiThread multithreading
IOS multithreading is relatively simple compared to Java, GCD can basically figure it out in a day. The Concurrency package for Java is more complicated.
Android, iOS UI development comparison
Apple officially recommends using Storyboard to develop UI. The advantage is that it is more intuitive, and you can better understand and maintain the App by looking at the interface. Now that Xcode7 has improved support for AutoLayout and SizeClasses, it’s easy to use multiple resolution interfaces, so I recommend that you drop the traditional approach of using frame code.
Of course, storyboards also have the following disadvantages:
Xib code is difficult to maintain. Xcode opens Storyboard or Xib to make changes to the file. Git will show that the file has changed even if no actual changes have been made.
Multi-person assistance while modifying leads to conflicts and merge difficulties.
Having too many pages in a Storyboard takes up too much memory, causing Xcode to stall and crash.
Error location is difficult, and the error message is not clear. It is difficult for novices to locate errors. For example, if you accidentally delete an IBOutlet, it’s hard to locate.
Storyboard UI development practices
Divided into storyboards by business modules, each one is responsible for avoiding overlapping modules.
Each storyboard should have no more than 10 pages, which can be redivided using the Refactor storyboard feature.
Use Containers to reuse UI modules in one storyboard and XIBs to reuse UI modules in multiple storyboards.
Complex input form, SwiftyForm framework is recommended to write code.
Multi-resolution adaptation is easier with Size Classes.
Android vs Swift iOS framework comparison
Swift frameworks are now available, and Swift can use the OC open source framework, but it is not recommended. We mainly compare the common network request framework, JSON parsing and image caching framework of the project.
In Android development, OKHTTP, Retrofit, Volley and other network frameworks are generally used for development; in iOS development oc era, AFNetworking library is used for development; In Swift development, Alamofire and Moya library are recommended.
Moya encapsulates the Alamofire network request library, so development does not need to write network model, management, etc. Make the code more concise. Moya can replace APIManager, a network abstraction layer written by Moya. Moya offers some nice features:
-
Compile time checks the correctness of API interface calls
-
Defines interfaces of different apis through enum enum types
-
Making the interface test stub a first-class citizen makes unit testing easy.
-
Supports ReactiveX extensions for easy integration with RxSwift.
-
Moya has more features to simulate data debugging than Android Retrofit.
Currently, App basically uses JSON as the packet protocol, and we generally use Gson for parsing in Android development. In Swift development, ObjectMapper, Argo+Curry, SwiftyJson are compared. We decided to use ObjectMapper as the JSON parsing framework. ObjectMapper supports the following features:
-
Support to convert objects to JSON and JSON to class objects
-
Support for nested objects (single objects, collections of object lists, and dictionaries)
-
Support for custom conversion functions
-
Support for structs
-
Support for Realm and Alamofire integration, AlamofireObjectMapper
Realm is a popular database for iOS developers designed for mobile, replacing SQLite and also available for Android.
Use examples:
let user = Mapper().map(JSONString)
let JSONString = Mapper().toJSONString(user, prettyPrint: true)
Realm is a popular database for iOS developers designed for mobile, replacing SQLite and also available for Android.
Image caching framework, Android development commonly used Glide and Fresco, OC development generally use SDWebImage, Swift development recommended use HanekeSwift.
Comparison of development tools
The main development tool for Android is Android Studio and Eclipse ADT until 2014. IOS development has always used Xcode. Xcode is difficult for Java/Android developers to get started with, especially Interface Builder, Xib, and view code that generate event methods directly through wires, which can be challenging and difficult to locate. Android developers are used to writing XML interface code by hand.
Compared to Android Studio, Xcode is fast but less stable, and it’s normal to crash several times a day. In terms of emulators, Android Emulator is a virtual machine with slow startup and installation speed, while iOS Simulator is fast, but some functions cannot be simulated. Are recommended to use real machine development.
Package manager vs. Build tool comparison
In the early stages of Android development, Ant was used for some tasks. Later, some teams borrowed from Java EE projects and used Maven. After Android Studio came along, Google recommended Gradle. Android’s build tools are much more powerful than iOS.
In the early stage of iOS development, CocoaPods was used. Now Carthage is recommended. In the future, Apple will promote the official Swift Package Manager after the official release of Swift 3.0.
The advantage of Carthage is that it is relatively simple, but the disadvantage is that some frameworks are not supported, especially some open source libraries of domestic BAT.
Finally, Alcatraz, the Xcode package manager, provides Xcode plug-ins, templates, and color modes. Compared to Android Studio, there are very few plug-ins.
Baichuan.taobao.com is alibaba Group “cloud” + “end” core strategy is Alibaba Group wireless open platform, based on world-class back-end services and mature business components, through the opening of “technology, business and big data”, Provide mobile entrepreneurs with solutions that can quickly build apps, commercialize apps and improve user experience; At the same time, it provides diversified entrepreneurial services – physical space, incubation operation, venture investment, etc., providing comprehensive guarantee for mobile entrepreneurs.
About Alibaichuan