Gergo Erdosi Gergo Erdosi - 6 months ago 21
Swift Question

Does Swift have access modifiers?

In Objective-C instance data can be

public
,
protected
or
private
. For example:

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


I haven't found any mention of access modifiers in the Swift reference. Is it possible to limit the visibility of data in Swift?

Answer

Edit: Access control mechanisms have been introduced in Xcode 6 beta 4 (6A267n):

Swift access control has three access levels:

  • private entities can only be accessed from within the source file where they are defined.
  • internal entities can be accessed anywhere within the target where they are defined.
  • public entities can be accessed from anywhere within the target and from any other context that imports the current target’s module.

I have been browsing Apple Developer Forums and found this thread, where @gparker (an Apple engineer) says that:

Swift currently has no access control mechanisms.

However, after a couple of complaints, he states:

We don't usually promise anything for the future, but in this case we are making an exception. Swift will have access control mechanisms.

That's certainly good news. :)

Comments