Identify the values and references in Swift, follow the bad references, the agent's point of attention

Source: Internet
Author: User

1.0 value types and reference types are divided in Swift

Int, String, struct, and enum all belong to a value type, passing a value type to the method, creating a copy of it in memory and passing the copy, so that we can modify it as we want, without worrying about modifying the incoming raw value.

When a reference type is passed, it is not copied, but its address is given to a function or method that might use them. Closures and the objects that are instantiated from the class are reference types. When a closure or an object is passed to a method, its copy is not created, but the reference (memory address) is passed. Because a reference type is passed without creating a copy of it, special care needs to be taken to ensure that it is properly freed at the right time, and that releasing memory prematurely can cause crashes and later lead to memory leaks.

2.0 Reference Loops

Here we use a simple example to illustrate briefly how a circular reference can be created in development.

Example: A man owns a dog, and a dog owns a master.

Directly on the code here the person class has a dog attribute, while Deinit is quite the dealloc function in OC

There's a master here in the dog class.

In the MAIN.SWIFTZ we use this

Run the program and we will see that the output station does not have any output .... And when we are in one of these types, when we add weak when declaring a property, things will be different. For example, run the program, it will print out, the person is recycled, the dog is recycled ...

From this example we can see that when using the Var declaration, the default is a strong reference, rather the strong in OC

In this case, the Var default strong reference, you think the proxy

At first, I wrote the Agent with Swift.

Personal feeling, yes, but in the OC environment has become

At this moment, with the design of Apple different Ah, the general agent properties should be weak ah, and then I found the Var delegate:lmtablbarviewdelegate? This sentence, after adding weak in front, is looking at the change

This is finally the result you want .... All of us should add the weak adornment before setting the proxy properties.

These are only personal views, if there are errors, I hope you can help to amend

Identify the values and references in Swift, follow the bad references, the agent's point of attention

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.