From Objective-C to Swift -- Swift candy (1)

Source: Internet
Author: User
Tags call back

Swift brings many really great features, making it difficult to return to Objective-C. The main feature is security, but this is also seen as an additional side effect.

Strongly typed APIs

Swift has a strong type, which means that, unless required, Swift will not convert between types for you. Therefore, for example, you cannot assign the Int type to the Double type. You have to first convert the type:

 
 
  1. let i: Int = 42 
  2. let d: Double = Double(i) 

Or you must extend a method to the Double class to convert the Int type:

 
 
  1. extension Double { 
  2.         func __convert(i: Int) -> Double { 
  3.             return Double(i) 
  4.         } 
  5.     } 
  6.     let another_d: Double = i 

Strong type is very beneficial for security. However, if it does not add a lot of type information to the type interface, it may become a daunting thing, a bit like writing a script language.

 
 
  1. let ary = ["Hello", "world"] // NOTE: 'ary' is of type String[] or Array<String> 
  2.     for s in ary { // NOTE: 's' is of type String 
  3.         print(s + " ") 
  4.     } 

If you want to create an array containing many types (without common ancestor), you should use enumeration (which can contain values, as shown below ). If you want it to include all values, you can use the Any type. If you want to include any Objective-C type, use the AnyObject type.

Note that the type interface does not add a type when declaring a function. You must explicitly describe the type of the function you declare.

Blocks

Blocks in Swift is similar to Blocks in Objective-C, but there are two differences: type inference and avoidance of weakify dance.

For type inference, you do not have to include the complete type information every time you write a block:

 
 
  1. sort([2,1,3], { 
  2.         (a: Int, b: Int) -> Bool in return a < b 
  3.     }) 
  4.       
  5.     // Using Type Inference 
  6.     // Using the Trailing Closures feature 
  7.       
  8.     sort([2,1,3]) { 
  9.         a, b in return a < b 
  10.     } 
  11.       
  12.     // Implicit 'return' for single-expression blocks 
  13.       
  14.     sort([2,1,3]) { a,b in a<b } 
  15.       
  16.     // Shorthand Argument Names 
  17.       
  18.     sort([2,1,3]) { $0 < $1 } 
  19.       
  20.     // Operators are functions, and functions are blocks too! 
  21.       
  22.     let sorted: Int[] = sort([2,1,3], <) 

Visit Closures to learn more about blocks.

In addition, the weakify dance of Objectvie-C is a little easy. You only need to add [unowned self] or [weak self] at the beginning of the block.

 
 
  1. class CallbackTest { 
  2.         var i = 5 
  3.         var callback: (Int -> ())? // NOTE: The optional callback takes an Int 
  4.         deinit { // NOTE: This is like -dealloc in Objective-C 
  5.             println("Deinit") 
  6.         } 
  7.     } 
  8.       
  9.     var obj = CallbackTest() 
  10.     obj.callback = { 
  11.         [unowned obj] // NOTE: Without this, deinit() would never be invoked! 
  12.         a in 
  13.         obj.i = a 
  14.     } 

Note that the call back on the Optional image is introduced in the Introduction post article ).

Refer to the ARC chapter for more information.

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.