Talking about the difference between swift and OC

Source: Internet
Author: User


Objective


Blink of an eye Swift3 come out almost a year, from OC to Swift also experienced a lot, so on the use of the difference also summed up a little, for the record, right when one of their notes.



Of course, some of the differences may be heard, so here will also combine their own some of the usual records, a little finishing, just born this article, the middle may have the wrong place, also hope points out, common progress.





Body


I. The link between swift and objective-c



Swift and Objective-c share a set of runtime environments, and the type of swift can be bridged to objective-c (hereinafter I call OC) and vice versa. The mutual reference of the two mixed programming I have previously written a brief introduction, interested can be here:


IOS OC and Swift make calls to each other.


The second is that many of the libraries accumulated before OC can still be used directly in swift, of course, after Swift3, some of the syntax has changed a lot, but there are still traces to follow. Most of the concepts that OC has ever seen, such as reference counting, ARC, properties, protocols, interfaces, initializations, extension classes, named parameters, anonymous functions, and so on, continue to work in Swift (possibly with a different term). Most of Swift's concepts are the same as OC. Of course Swift also has a number of emerging concepts, which are not in OC, such as model, tuple and so on.









Ii. What are the advantages of swift than objective-c?



1, Swift easy to read, grammar and document structure simple.



2. Swift is easier to maintain and the structure is clearer after file separation.



3, Swift is more secure, it is a type-safe language.



4, Swift code less, concise syntax, can save a lot of redundant code



5, Swift speed, higher computational performance.









Iii. shortcomings of Swift's current existence



1, the version is not stable, before upgrading Swift3 big move knife, bitter a lot of people, SWIFT4 is still unknown



2, the use of a low proportion of the current OC is still the world



3, the community of open-source projects less, after all, OC alone for many years, many excellent libraries do not support Swift, but this situation is changing, there are many excellent Swift open source class library



4, the proportion of companies used is not high, many companies to stabilize the main, or in the use of OC development, few in the development of hybrid, less is pure swift development.



5, occasionally in the development of some problems encountered, it is difficult to find relevant information, this is a disadvantage.



6, the operation of pure swift and OC have essential differences, some of the powerful functions of the runtime, in pure Swift has become invalid.






Iv. Other features of Swift



1, Swift's memory management



Swift uses automatic reference counting (ARC) to simplify memory management, consistent with OC.



2, the optional type of Swift (Optionals) Introduction



Swift introduces optional types to handle situations where the value of a variable does not exist. Optionals is similar to the pointer to nil in OC, but applies to all data types, not just classes, optionals is more secure and concise than the nil pointer in OC, and is at the heart of Swift's many most powerful features.



3, in Swift! And?



These two symbols are used to mark whether the value of this variable is optional! Indicates that the optional variable must ensure that the conversion succeeds, otherwise the error, but the defined variables can be used directly; Represents an optional variable, even if the conversion is unsuccessful and will not error, the value of the variable is nil, if the conversion is successful, you need to use the variable later! Be modified.



4. Simple description of the model in Swift



The paradigm is used to make the code work safely, and the paradigm in Swift can be used in function data and ordinary data types, such as classes, structs, or enumerations. Paradigm can solve the problem of code reuse,



A simple example: these two methods are very similar, mainly on the difference between a parameter type.


   func isIntEqual(x:Int,y:Int) -> Bool { return x == y
    }
    
    func isStringEqual(x:String,y:String) -> Bool { return x == y
    }
View Code


We can combine the patterns with the paradigm:


  func isObjEqual<T:Equatable>(x:T,y:T) -> Bool { return x == y
    }
View Code


In this way, when we use the two different types of numbers to compare, the compiler will immediately remind us to avoid this situation quickly.









5. Change of access rights for Swift



Swift has two new access rights and more granular permissions. See here for details:



Access rights are from large to small : open,public,internal (default), Fileprivate,private









V. Other SUPPLEMENTS



========================= Swift Exclusive ====================



1. Range operator



A...b said that [A, a,] includes a and b. (such as 3 ... 5 is the range take 3,4,5)



A.. <b means [A, a, b] includes a, not including B. (such as 3 ... 5 is the range take 3, 4)



Common such as for loop: For I in 0...9{}






2. Unique tuple types



Tuples (tuples) combine multiple values into a single composite value. Values within a tuple can be of any type and are not required to be of the same type. eg


var value = (int,string) = (x:all, y: "abc")





3. Use let to define constants in Swift, Var to define variables



The use of constants, more secure, can not be modified, when the need to modify the object can only be modified with Var .









========================= Details Use difference ====================



1. Swift does not divide. h and. m files, one class only. Swift one file, so the overall number of files is somewhat less than OC.



2. A semicolon is not required at the end of a swift sentence unless you want to write three lines of code in a single line separated by semicolons.



3, Swift data type will be automatically judged, only the variable var and constant let



4. Coercion type conversion format different OC strong turn: (int) A swift strong turn: Int (a)



5, about the type of bool more stringent, Swift is no longer OC is not 0 is true, but true is real false is false



6, Swift's circular statement must add {} Even if only one line of code must be added



7. Swift's switch statement can be followed by a variety of data types, such as floating point, string lines, and there is no need to write break (OC does not seem to be a string)



8, the parentheses after Swift if can be omitted: if A>b {}



9. Print ("\ (value)") when printing a variable in swift print ("")












Vi. Prospects for Swift



Perhaps many people will ask, in the end should not learn swift, very difficult?



My understanding is:



If time can, can try to learn, it is best to try to write a little code, simply read or read the tutorial does not make much sense, the practice of knowledge.



In fact, the most early learning, more is the change of grammar, the difficulty here is not big, and Swift thought we can learn slowly later and deeply experience.






Finally, I hope that Swift will grow better, more functional and better community.









"Not to be continued ... 】



enjoy~






Talking about the difference between swift and OC


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.