Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Understanding access modifiers in Swift

Adding initializers to a class Добавление инициализаторами к классу | Using inheritance | Using inheritance Использование наследования | Creating computed properties | Using inheritance Создание вычисляемых свойств | Using type properties and methods | Using type properties and methods Используя свойства и методы типа | Defining lazy properties | Defining lazy properties Определение ленивых свойства | Adding property observers |


Читайте также:
  1. Accessing the Special Functions
  2. Creating loops in Swift
  3. Creating loops in Swift Создание петель в Swift
  4. Http://www.maskworld.com/english/products/costume-accessories/decoration--3000/halloween-decoration--310/--ES-18122
  5. Load-lifting mechanisms and accessories
  6. Special accessories, instruments and tools
  7. The structure of Swift

 

- And the final thing to cover in this section is access levels or access modifiers. It's common in many object oriented languages to have key words like private and public that can be attached to your classes, to your methods and properties, to control how visible and usable they are from elsewhere in the application or even from other applications. In Swift we have three such keywords, public, private, and internal. Marking a method or property or class as private means it's now only accessible from within the same source code file.

 

Attempting to call it from anywhere else will fail. Now internal means that this class, this method, or this property, is accessible from any code that's compiled into the same module. Now this is the default level for Swift, meaning that in a typical Xcode project you can access classes, properties, methods, that are written in separate code files as long as they're all part of the same project and compiled together into the same module. So if you don't use any of these keywords, any of these access modifiers, it is as if you'd written internal on your classes, on your methods and properties.

And then there's public that opens things up a bit. Marking a class as being public with public methods means that it's also callable from outside your own compiled module so by any other module that has imported your code. This is the term you would use if you were creating say a framework that you wanted to be reusable across multiple different projects. To use these, we just use one of these keywords in front of the method or class or property we want to affect. And one thing to understand about this is the levels of nesting that are going on.

You can't, for example, just mark a single property or method as public if you haven't also marked the class as public. It doesn't make sense to say you have a publicly accessible method but the class it's in is internal and not publicly accessible. As a general principle of object orientation, you'll want access levels that are on the restrictive side and only allow outside access to any data or behavior when it is a proven need.

 

 


Дата добавления: 2015-08-20; просмотров: 53 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Adding property observers Добавление наблюдателей собственности| Understanding access modifiers in Swift

mybiblioteka.su - 2015-2024 год. (0.006 сек.)