Swift 3 Migration Detailed

Source: Internet
Author: User

Write in front
    • The SWIFT 3.0 release has been released for almost one months, with intermittent handles and swift-related migrations to Swift 3.0. So write a little summary.

Amount of background code (40,000 lines)
    • First of all, I began to start Swift at the beginning of this year. Coupled with Swift's ABI and API has been unstable, so there is no large-scale use of the project, so this migration of code is not much, about 40,000 lines .

Migration Time (around One Day)
    • It took about 1 days to move in Time. Two mixed projects, a Swift-based project. Mid-term Swift-based projects took about a half- day , with two mixed codes, but one spent half a day , and one was almost only spent for an hour ( Reason to leave a suspense first ~).

Get ready

Many of the decisions that were made during the initial development of the choice of Swift also left me with a lot less work this Time.

Interface with xib instead of pure code
    • quirks, most of the interfaces associated with Swift are painted with xib. And this xib has a great advantage in this migration, xib and SB's code does not fit with Swift 3. If you want to use the code to write the UI, the migration changes are estimated to be much more.

About the selection of Third-party libraries:
    • For a project, The Three-way library seems to be a must-choose dish , but how to choose this dish?

    • For the Three-party library, the original choice is that you can use OC to try to use OC. After all, OC can seamlessly connect to Swift, and it is relatively stable.

    • When choosing a Three-party library related to Swift , I try to value more libraries, such as alamofire,Snap,Kingfisher,Fabric And so on, because the user is more, the developer will be more willing to maintain, do not skip Ticket. So there is no problem for many small partners now, want to migrate, but some libraries are not updated. At least for me, when I want to migrate, all the Three-party libraries associated with Swift have migrated to 3.0.

Thanks to the above two points, there is a lot less work to do during the migration process.????

Knowledge Reserve Upgrade
    • Let's look at the changes in Swift 2 to Swift 3 and the reasons for the Change. (10,000 Grass mud Horse flies through the heart, but in fact it is getting better)

      • Swift official Bo

      • Swift-evolution

      • Swift 3 new features at a glance

    • Then the grammar document was quickly revisited.

      • Swift Programming Language

      • Chinese version

Issues in migration any && anyobject
  • I would like to do the migration and the migration of the students to change the most one is as AnyObjct? it?

  • At least for me, Yes.

  • The
  • And this related basic are collection types . In Swift 2 we used a   [anyobject]   to hold any variable, even the String , Array for the struct type   and so On. But according to the reason Swift's   anyobject   refers to a class, and   any   is the struct , class , func   all Types. But why   Struct   can be put into   [anyobject]  ? In   Swift 2   will be for String , Int   and so on   Struct   makes a implicit bridging Conversions. and to   Swift 3   has a **fully eliminate implicit bridging conversions from swift** Changes.

  • Of course in my project [anyobject] is actually small, the most troublesome is [string:anyobject]. Because when writing the project, or in the OC to Swift stage so for Dictionary, the basic use of [string:anyobject], so in the revision, in many places for this change.

    • At first, I followed Xcode's hint and added a Dictionary behind the Value.as AnyObjct?

    • Then gradually found that I did a very silly thing, in fact, I just put [string:anyobject] to [string:any] on it.????

  • That's Why I spent so much time modifying the code in the first mixed project! Thanks to the second project of the mixed study, Yep's idea is to [String:AnyObject] name a type called JSONDictionary . So in any && anyobect This thing, it took a little time.

Swift 2 var json = [string:anyobect] () json["key1"] = 1 json["key2"] = "2"//to Swift 3 Step 1 var json = [STRING:ANYOB ect] () json["key1"] = 1 as anyobject?json["key2"] = "2" as anyobject?//to Swift 3 Step 2 var json = [string:any] () Json["ke Y1 "] = 1 json[" key2 "] =" 2 "//swift 2 public typealias jsondictionary = [string:anyobject]//to Swift 3 Step 2public type Alias jsondictionary = [string:any]
Alamofire and other Three-party library support iOS8
    • Although I used the Three-party library in the first time to upgrade the library to   Swift 3   but midterm   Alamofire   and   Snap   Two library minimum adaptation only to IOS 9, in order to avoid and product tear force, have to find a way to solve this adaptation problem. The following   Alamofire    For example

    • In fact, the Three-party library, not necessarily only with COCOAPODS. So plan to download the code and then direct the Source.

    • First Alamofire is modified to a minimum of 8.0, and then compiled to find a function that does not pass, and Delete. (in fact, These functions are all new IOS 9 functions, So the deletion does not affect anything.)

    • It takes about half an hour or so to erase, and then just drag it to the Project.

    • Snap   Just drag it in, okay? There is no need to modify anything at the Moment.

In Fact,!os (WATCHOS) this macro under the # If!os (watchOS) @discardableResultpublic func stream (withhostname hostname:string, port:int )-streamrequest {return SessionManager.default.stream (withhostname:hostname, port:port)} @discardableResultpub LIC func stream (with netservice:netservice), streamrequest {return SessionManager.default.stream (with:netservic e)} #endif
@escaping
    • This is the most painful pit i've had in my fitness Match.

    • First look at Swift-evolution just understand @escaping must show the Declaration. however, It is not known that @escaping closures can be modified in the function Body.

Let pedonmeter:cmpedometer = cmpedometer ()     func  Getpedometerdatafromdate (_ datet:date?, withhandler handler:  @escaping   ( Cmpedometerdata?, error?)  ->  ()) {        //  Compile error          pedonmeter.querypedometerdatafromdate (starttime, todate:endtime, withhandler : {  (pedometerdata:cmpedometerdata?, error:nserror?)  -> void in            guard  let pedometerData = pedometerData else { return }             handler (pedometerdata, error)              //  do some things         &nbsp,})         //  Last Resort onlyCan not be modified, the function outside to do something         pedonmeter.querypedometerdata (from:  Starttime, to: endtime, withhandler:  handler as! cmpedometerhandler)      }
Result of call to ' funtion ' is unused
    • This is not actually a compilation error, but the warning was a bit confusing at First. return value don't you want me to change it?

    • In the beginning, I was so modified let _ = funtion() , but the back in the look at the SE-0047 when @discardableResult it is possible to achieve this Effect.

Date && NSDate
    • Because there is a project that uses the datetools Tool. It has a classification of NSDate + Tools .

    • But in the process of writing Swift 3, I discovered that if a variable is of type date, you cannot use NSDate + Tools , you must display some methods that declare date as NSDate so that you can call the Classification.

    • This allows the use of OC library when it feels very uncomfortable, after all, a lot of NS prefix removed. All the statements are too unfriendly to show.

Caanimationdelegate
    • This actually seems to be a modification of Xcode 8. Because before Caanimationdelegate is a Classification. The following is probably stated:

@interface nsobject (caanimationdelegate)-(void) animationdidstart: (caanimation *) anim;--(void) animationdidstop: ( Caanimation *) anim finished: (BOOL) flag; @end
    • Before it was in the VC just rewrite the animationDidStart function can Be. But the new is not, initially thought to be Swift 3 change, but in fact is the modification in Xcode 8. Turning Caanimationdelegate into a protocol. I feel that this modification is for the purpose of matching Swift 3? The changes are as Follows:

@protocol caanimationdelegate @optional-(void) animationdidstart: (caanimation *) anim;--(void) animationdidstop: ( Caanimation *) anim finished: (BOOL) flag; @end

Because the width of the time is longer, the other temporarily can't think Of. It's not too soon to be continued ...

Other
    • There are many subtle changes that make it seem like you don't understand the language, so it's recommended to take a look at the following article before Adapting.

      • Swift 3 new features at a glance

      • [Swift 3.0-released on september, 2016]https://github.com/apple/swift-evolution/blob/master/releases/swift-3_0.md

      • And the Swift 3 must-see series of students @ Zhuo

    • And a couple of good summaries.

      • Swift 3 by Gupeng

      • A little bit of experience with Swift 3 and pit by Tula Tripod

Summarize
    • Overall this migration is not as painful as it might seem, although the proposal has changed a lot, but thanks to the Xcode 8 migration tool, This migration takes a little time, and of course it may be related to my Code size ~

    • After the migration, look at the code, you will find that Swift more elegant, at least compared to the second agreed a lot, as good as where? you'll know if you write it yourself.

    • finally, can finally put the XOCDE 7 uninstall, no longer worry about two open no brain flash back!!!

    • Finally for next Year's Swift 4 Just want to say come on ~ ~ Minute to solve you!

    • In fact, the path of adaptation is just beginning, because the Xcode 8 auto-transcoding code does not have a very good Swift 3. At the moment it's just that the Swift 3 can be compiled and Passed.

More

After work, write some notes, if necessary can be seen on my GitHub



Article transferred from Damonwong's Pinterest

Swift 3 Migration Detailed

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.