Introduction to access Control permission controls in Swift

Source: Internet
Author: User
Tags ming uuid


This article mainly introduces the access control in swift, this article explains the use of private, internal, public three keywords, the need for friends can refer to the



If you've never had access to permission control before, listen to a little story:



Xiaoming is a freshman of five crossing Industrial College, recently he is a little annoyed, because the roommate often use his hot water pot, as if it is his own home, but due to the classmate sensibilities, and embarrassed to say. Until one day, he and learn elder sister small K spit Trough.



After listening to the elder sister, said: College Collective, most of the things are the default roommate can share. If you don't want someone else to take it, I can help you seal it, as long as you have a private tag, they can't see your things and they won't be able to use your stuff.



Xiao Ming said that by learning elder sister You can also demon law ...



The swift language, from the Xcode 6 Beta 5 release, has added support for access control. In fact, the rights control and Xiao Ming's items, you can set the kettle is only the use of their own, or only the people in the dormitory can use, or the whole school can be used.



From then on, you can be like the director of the Aegis, take full control of your code block's "confidentiality level", which is only referenced in this document, which can be used throughout the project, you can also play the Spirit of love, open it up as long as the introduction of your framework, you can use the API.



These three types of permissions are:



# # # # #private Private #



Where it is written, where it is used. Whether they are classes, variables, constants, or functions, once they are marked as private, they can only be used in the source files that define them and cannot be used for other files.



# # # # #internal inside the



Blocks of code marked as internal are accessible across the entire application (app bundle) or within the framework (framework).



# # # #public the public



Blocks of code that are marked public are typically used to create APIs, which are the most open permissions that anyone can access using if they import the module.



If you want to add all the love to a deadline, oh no, is to give all the code blocks are labeled permissions, not tired of the blame. Fortunately, the default permissions for all code entities in Swift are the most commonly used internal. So when you develop your own app, you may not be able to control things in the right way.



But when you need to write a public API, you have to make the code blocks inside to "stealth-visible", or else they won't.



Private-level permissions are the strictest, and can be used to hide the details of how certain features are implemented. By building your code properly, you can safely use extension and advanced features without exposing them to other files within the project.



In addition to having permission to set the entire declaration, Swift allows you to set a property's value permission more open than the assignment permission when needed.



# # # #example:



The code is as follows:



public class ListItem {



ListItem This class, there are two exposed properties



public Var text:string



public Var Iscomplete:bool



The following code indicates that the assignment permission for the variable UUID is set to private, readable for the entire app, but the value can only be written in this file



Private (set) var uuid:nsuuid



Public init (text:string, Completed:bool, Uuid:nsuuid) {



Self.text = text



Self.iscomplete = Completed



Self. UUID = UUID



}



This paragraph does not have special flag permissions and therefore belongs to the default internal level. Available within the framework target, but not available for other destinations



Func refreshidentity () {



Self. UUID = Nsuuid ()



}



public override Func IsEqual (object:anyobject?)-> Bool {



If let item = object as? ListItem {



return self. UUID = = Item. Uuid



}



return False



}



}



When we use the OBJECTIVE-C and swift hybrid development, we need to note:



If you are writing an application, Xcode will generate a header file to guarantee the accessibility of both, and this generated header file will contain public and internal level declarations.



If your final product is a swift framework, only declarations marked as public are present in the header file. (Because the header file of the framework is part of the exposed Objective-c interface, only the public part is available to objective-c.) )



Although Swift does not recommend the dissemination and use of third-party frameworks, it supports the framework for the creation and sharing of source documents. For developers who need to write a framework for easy application and multiple projects, remember to mark the API as public.



If you want to learn more about permission control, you can view the latest Apple's official "the Swift Language" and "Using Swift with Cocoa and objective-c" guidelines,



These two guides can be downloaded and updated in ibooks.


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.