The effect of the access modifier in Swift on the extension (extension)

Source: Internet
Author: User
Tags modifier modifiers

In Swift, there is a structure extension, similar to the category category in OC, which is ideal for managing code

At the same time, common access modifiers have public, internal, private, and for these three access modifiers, the impact on extension is different

In this article, I will describe a total of four scenarios for extension under the same file and different files, in this class and other classes.

Before I introduced it, I created a very simple iOS project and built a new extension file for the person class and person class, such as:

Where the code for the Person class is as follows:

The code in the Person+extension file is as follows:

As you can see from the code above, extension created two functions in the person class in this file, namely Getage () and Getgender ().

Two functions were created in the Person+extension file called SayHello () and Saygoodbye ()

The specific tests will be performed separately in the Viewdidload () two functions of the printsomething () and Viewcontroller classes of the person.

1, internal

Internal is a scope between public and private access modifiers, and is the default access modifier in swift, usually omitted directly

Internal is neither as open nor as private as public.

A. Extension under the same file, called in other classes:

In the Viewcontroller file, call the person class directly in this file extension in the following way:

It is clear to see that it is possible to call this function very directly.

B. Extension under different files, called in other classes:

Next, call Person+extension's SayHello () function:

In this case, functions in extension that are not the same file can also be called to the

C. Extension under the same file, called in this class:

The test results in the person class are as follows:

d. Extension under different files, called in this class:

The test results in the person class are as follows:

Conclusion: In the case of internal or no modifier, whether the function and the class file in the extension are in the same file, the execution can be called smoothly.

2. Private

Private methods/functions are represented in each programming language, where we try to use the extension function in both cases

Before you begin, please

A. Extension under the same file, called in other classes:

According to the test, even if the extension is in the same file as the class, it cannot be called to the private decorated function in Viewcontroller getgender ()

B. Extension under different files, called in other classes:

After the test results show that the extension and the class in different files in the case, Viewcontroller also cannot call to the private modified function Saygoodbye ()

C. Extension under the same file, called in this class:

Tested, in this case, this class can be called to a function that is private decorated with a class in extension under the same file

d. Extension under different files, called in this class:

The test results show that the private-decorated function, which is not in the same file as the class extension, cannot be called even in this class

Conclusion: In the private modified extension function, only the same file as the class can be called smoothly, in other cases cannot be called

3. Public

Public, because of its openness, can call a public-modified method/function anywhere, so it is often used for global variables, properties, or methods.

Before testing, add the public modifier to the Getage () and SayHello () functions that were not previously using the access modifier.

In the actual test, the public modifier immediately before the method prompts the following warning:

In this case, the Xcode compiler recommends replacing public with internal, which, in order to solve this problem, must also be public before the class declaration

This proves that a public -modified function can exist only in a class that is modified by public

After the revised test, the test results are as follows:

A. Extension under the same file, called in other classes:

B. Extension under different files, called in other classes:

C. Extension under the same file, called in this class:

d. Extension under different files, called in this class:

Conclusion: A function that is modified by public, whether or not in the same file as this class, can be called in this class and in other classes

All the conclusions are as follows:

Access modifiers Same file, other class calls Different files, other class calls The same file, this class calls Different files, this class calls
Internal can be called can be called can be called can be called
Private No No can be called No
Public can be called can be called can be called can be called

Table notes: The same file and different files represent only the same file as the extension in which the function resides

Reference article: Three subtle details of the https://github.com/bboyfeiyu/iOS-tech-frontier/blob/master/issue-4/Swift extension. md

The effect of the access modifier in Swift on the extension (extension)

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.