Swift closures and proxies for simple use _swift

Source: Internet
Author: User
Tags closure
A brief introduction to the use of agent design patterns in swift and the value of closures

First, the agent mode

The agent simply means to let others help themselves to do something that is not or inconvenient to do.

The following steps are implemented for the agent:

1. Write a protocol, define the proxy method, is what needs to be done

2. Two objects (a, B) assuming B is the agent of a

3.A requires an attribute delegate

4.B comply with A's agreement, set agent Relationship a.delegate = B

5.B Implementation Agent Method

6.A Trigger Agent Event

As follows: Declaration protocol

Agent Properties

Comply with the agreement

Set up agent relationships

Implementing Proxy methods

Triggering agent Events

The above is the process of the agent, but the declaration of the above agreement, it must be implemented, and the OC inside similar @optional effect can be so written

Second, the closure of the simple practical and transfer value

1. Use Typealias to alias closures

2. Defining attributes

3. If not Typealias can write directly

4. Call closure, pass parameters

5. Implement closure, receive parameters

The above is the closure of the value of the packet, and the implementation of the OC basically the same.

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.