Skip to content

Latest commit

 

History

History
128 lines (76 loc) · 3.13 KB

File metadata and controls

128 lines (76 loc) · 3.13 KB

Access Specifier 1 ~ 5

1) What is Access Specifier? What are the access specifiers available in Swift?

Answer:

  • Access Specifiers are the way to specify the availability scope of Variables and Methods.
  • final
  • public
  • private
  • fileprivate
  • open
  • internal

2) What is the default Access Specifier in Swift?

Answer:

  • Internal

3) private vs fileprivate access specifier?

Answer:

  • private variables are accessible only within the class.
  • fileprivate is accessible in the entire file. Mainly useful for extensions.
class A {

	private var aPrivateVar: Int = 10

	fileprivate var aFilePrivateVar: Int = 20

	func aMethod() {
	    // private can be accessed within the same class
			print(aPrivateVar)
	}

}

class B: A {

	func bMethod() {
			// print(aPrivateVar) // Error!
			print(aFilePrivateVar) // You can access fileprivate
	}

}

4) public vs open

Answer:

  • Public method or variable can be accessible anywhere but public class defined in another module can’t be extended.
  • Open method or variable can be accessible anywhere but open class defined in another module can be extended.
  • An open class is accessible and subclassable outside of the defining module.
  • An open class member is accessible and overridable outside of the defining module.
  • public class is accessible but not subclassable outside of the defining module.
  • public class member is accessible but not overridable outside of the defining module.

5) What is final keyword?

Answer:

  • A class defined as a final can’t be inherited but extended.
final class A {

}

// Error
class B: A {

}

Table Of Contents

Section 1, Data Type

Section 2, Operator

Section 3, Conditional Statement

Section 4, Enum

Section 5, functions

Section 6, struct

Section 7, initializers

Section 8, closures

Section 9, OOP

Section 10, static type vs dynamic type

Section 11, optional

Section 12, generic

Section 13, subscript

Section 14, access specifier

Section 15, higher order function

Section 16, delegate

Section 17, extension

Section 18, Memory Management

Section 19, protocols

Section 20, collections

Section 21, KVO and KVC

Section 22, Exception Handling

Section 23, Framework

Section 24, Objective-C