From now on, start Swift

Source: Internet
Author: User

Lead

Apple introduced Swift's new programming language in the 2014 WWDC, a moment of headlines for major websites and a hot topic for all programmers, a language that incorporates the benefits of a variety of programming languages, compared to the less readable objective-c, which is recognized by developers of major programming languages, and eager to learn the new language, and become a master of the iOS development platform programmer.

However, there are a group of people in the silent, is that we have been abused by Apple tens of thousands of times, we have to treat it as the first love of the iOS developer

    • "Alas, a big wave of people are approaching."
    • "Hurry to learn, or you can't keep up with the times."
    • "What about the previous code, what about the previous class library?"
    • "Not to learn." I continued to use the Objective-c "
    • ......

But you are still in the Rihanna knot, already have a lot of developers from the date of the release of the Swift journey, and it turns out that this is a good language, and OC can be a perfect combination, so you are waiting for what, do not wait until the next official project, now, whether you are novice or veteran, In the demo or the official project, is the next command + N, select swift!

About Swift's syntax please go to official documents or translate

Start Swift

Development tools: XCode6

The engineering environment isObjective-C

Create a new single View application , named swiftTurialDemo1 , lauguage select objective-c * *,

Add the afnetworking class Library, if you have installed the Cocoapods as follows, do not add it according to the traditional method.

To add to it Podfile , the content is

Pod ' afnetworking ', ' ~> 2.3.1 '

Close the project, use CocoaPods the installation, reopen the swiftturialdemo1.xcworkspace

One of the projects is OC Code control, we do a simple VC transfer value, and use afnetworking to request network data once, learn how to invoke the OC code in the swift file and how to invoke the SWIFT code in the OC file

Add a navigationcontroller to the Main.storyboard as the Initial View Controller and Viewcontroller as its rootviewcontroller

Command + N Create a new Swift filenamed secondviewcontroller, click Nextand a popup window will appear would Configure an objective-c bridging header? This file is to bridge the objective-c code so that you can invoke the OC code in the Swift file, so choose YES

Open swiftturialdemo1-bridging-header.h, which contains the classes that need to be called in the swift file, adding code:

#import <AFNetworking/AFNetworking.h>
Calling OC's code in Swift

Open Secondviewcontroller.swift

Replace with the following code

Import Uikitclass Secondviewcontroller:uiviewcontroller {var astring:string!//declares a variable, if it does not assign an initial value, it will be an error, add at the end? or! Is it optional, add?        , it is not allowed to call a method or property of string before Astring is assigned, add! You can do it without assigning a value. Override Func Viewdidload () {super.viewdidload () Self.title = "Second" println ("a string = \ (astring)") test ()//same as the C language call function} Func Test () {L ET urlstring = "http://www.baidu.com" var url = nsurl (string:urlstring)//Set URLString to the interface address that needs to be requested Var requ EST = nsurlrequest (url:url)//alloc init operation in Swift the initialization method for the class name () is shown in parentheses when the initwith ... Method gives the hint list var operation:afhttprequestoperation = afhttprequestoperation (request:request) Operation.respo Nseserializer = Afjsonresponseserializer () operation.setcompletionblockwithsuccess ({operation, responseObject)-& Gt void in println ("Success") print (Responseobject)}, Failure: {(operation, error), void In println ("Failure")}) Operation.staRT ()}} 

Open Main.storyboard , add a viewcontroller , set the class to Secondviewcontroller, in viewcontroller , change the title to "Jump" and add a segue that jumps to Secondviewcontroller .

Then run the project, click Jump, print out

A string = Nilfailure
Calling Swift code in OC files

To call the SWIFT code, like the SWIFT call OC, also need to bridge the file, just this bridge file, Apple has helped us to create, called "Your project name-swift.h", you do not see in the project, but the import after the tracking can be seen this file, The conversion of Swift corresponding to the OC code is done inside.

Open secondviewcontroller.swift, add a method at the end of class

Func log () {        println ("log Success");    }

Open viewcontroller.m, add

#import "Swiftturialdemo1-swift.h"
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{    uiviewcontroller *VC = [Segue Destinationviewcontroller];    if ([VC Iskindofclass:[secondviewcontroller class]]) {        Secondviewcontroller *SECONDVC = (Secondviewcontroller *) VC ;        Secondvc.astring = @ "pass a value";        [SECONDVC log];}    }

Run again, print out the result as

Log Successa string = Pass a valuefailure

You can download this code here

Fang Tsai said:

Although Swift can be mixed with OC, but this is based on separate files, it means that you can call each other as described above, but you cannot write OC code in swift, or write Swift's syntax in an OC file, with fun, iosers!

From now on, start Swift

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.