The main differences between OC and Swift

Source: Internet
Author: User

The main difference between Swift and OC is the grammatical differences, the others are probably the same .

Difference 1: use let to define constants in Swift, VAR defines variables. Once a constant is assigned, it cannot be modified, and the variable can be changed after it is assigned.

Swift can tear down the exact type of the variable according to the code on the right.

are there more constants or variables used in real -world development?

The use of constants, more secure, can not be modified, when the need to modify the object is modified to the Var modifier difference.

2: Swift has strict data requirements and does not make implicit conversions;

It is not possible to directly calculate between different types need to manually convert the type, call the constructor pass parameter for type conversion

Difference 3: One of the major features of Swift is the optional option, which defines whether the variable can have a value of a specified type, or nil, if an optional option is specified;

    1. Disclaimer: Var a:int?
    2. ‘?‘ Represents an optional type or optional unpacking,

      Optional unpacking is used when invoking the properties or methods of an object of an optional type

    3. '! ' means forced unpacking
    4. Gets the non-null value of an optional type
    5. Forced unpacking is risky and needs to be considered safe

'?? ' Merge Empty Options

    1. Need to use () to increase priority
    2. If there is a value to use directly, if not used?? Default values that are set later
    3. Judge a string or basic data

Difference 4: The constructor in Swift can be overloaded, that is, init parentheses write a bunch of parameters, oc in lieu of the way initwithxxx ....

Closed Package

    • String
      • String is struct, more lightweight, more efficient, supports traversal, can and nsstring seamlessly convert as
      • NSString Integrated from NSObject is an object
      • Merging of strings
        • Use +
        • Instantiating a string using string () arguments
        • \ () Escape string
        • Format the string (format:
      • Interception of strings
    • Collection
      • Let declares an immutable set, Var declares a mutable set
      • Array
        • [XXX] Represents an array type, XXX is a class
        • [XXX] () to instantiate an empty array
        • [element 1, Element 2,....]
      • Dictionary
        • [Key1:value1, Key2:value2,...]
        • [String:any] Most commonly used dictionary types
    • The basic format of closures
      • No parameter no return value (), ()
      • There are no return values for parameters (int, int), and ()
      • int (int, int) with parameters with return value
        • Trailing closures
          • When the last parameter of the function is a closure, the function's parameter ' () ' can be closed prematurely, if the function is only a closure parameter, ' () ' can omit

The main differences between OC and Swift

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.