Swift Quick Start delay call and Paradigm

Source: Internet
Author: User

Defer

Defer is like a final block of code in other languages, and after all other code in the function, it runs before the function returns. Regardless of whether the function throws an error, its code can run. As follows:

varfalselet fridgeContent = ["milk""eggs""leftovers"]func fridgeContains(itemName: String ) ->  Bool  {            true            defer {                false            }            let result = fridgeContent.contains(itemName)            return result}//调用函数fridgeContains("banana")print(fridgeIsOpen)

The value of the Fridgeisopen output is always false.

Fan type

As with paradigms in other languages, a pattern is created by writing a name in angle brackets after the name of the type or method:

//返回是一个数组func repeatItem<Item>(item: Item,numberOfTimes: Int) -> [Item]{    //创建返回数组    result = [Item]()    forin0 ..< numberOfTimes {        //在0到numberOfTimes-1的范围内重复添加对象        result.append(item)    }    returnresult}

Item is a type, and when this method is called, the item is exactly what type:

repeatItem("knock", numberOfTimes:4)

When this function is called, the auto-follow parameter gets the item's specific value, here is a string.

You can make a paradigm of functions, methods, classes, enumerations, structures, and so on.

//重新实现Swift标准库中的optional类型enum OptionalValue<Wrapped>{    caseNone    case Some(Wrapped)}var possibleInteger:OptionalValue<Int>=.None=.Some(100)

After the type name, you can use where to specify the scope of the specific type, such as the type must implement a protocol, require that both types must be the same, or require that the type must be a subclass of a class.

Func Anycommonelements <T: Sequencetype,U: Sequencetype where T.generator.Element: equatable,t.generator.element = = u.generator.element> (lhs:t,_ rhs:u) Bool { forLhsiteminchlhs{ forRhsiteminchRHS {ifLhsitem = = Rhsitem {return true}        }    }return false}//Call this function anycommonelements ([1,2,3],[3])

The function is to compare the items in the two array, return TRUE if any of the two arrays are the same, or return false.

The model has two parameters, all of which implement Protocol Sequencetype, which indicates that both parameters are aggregate classes or structures, such as arrays. After where the first condition is that the items of the collection class must support the "= =" and "! =" operators, the second condition requires that the elements in both collections must be of the same type.

Complete the full text.

Previous: Swift Quick Start optional type and error handling

Swift Quick Start delay call and Paradigm

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.