Does Swift has access modifiers? -


in objective-c instance data can public, protected or private. example:

@interface foo : nsobject {   @public     int x;   @protected:     int y;   @private:     int z;   } -(int) apple; -(int) pear; -(int) banana; @end 

i haven't found mention of access modifiers in swift reference. possible limit visibility of data in swift?

as of swift 3.0.1, there 4 levels of access, described below highest (least restrictive) lowest (most restrictive).


1. open , public

enable entity used outside defining module (target). typically use open or public access when specifying public interface framework.

however, open access applies classes , class members, , differs public access follows:

  • public classes , class members can subclassed , overridden within defining module (target).
  • open classes , class members can subclassed , overridden both within , outside defining module (target).

// first.framework – a.swift  open class {} 

// first.framework – b.swift  public class b: {} // ok 

// second.framework – c.swift  import first  internal class c: {} // ok 

// second.framework – d.swift  import first  internal class d: b {} // error: b cannot subclassed 

2. internal

enables entity used within defining module (target). typically use internal access when defining app’s or framework’s internal structure.

// first.framework – a.swift  internal struct {} 

// first.framework – b.swift  a() // ok 

// second.framework – c.swift  import first  a() // error: unavailable 

3. fileprivate

restricts use of entity defining source file. typically use fileprivate access hide implementation details of specific piece of functionality when details used within entire file.

// first.framework – a.swift  internal struct {      fileprivate static let x: int  }  a.x // ok 

// first.framework – b.swift  a.x // error: x not available 

4. private

restricts use of entity enclosing declaration. typically use private access hide implementation details of specific piece of functionality when details used within single declaration.

// first.framework – a.swift  internal struct {      private static let x: int      internal static func dosomethingwithx() {         x // ok     }  }  a.x // error: x unavailable 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -