ndarkness
ndarkness

Reputation: 1089

Can an internal class have public functions and variables in Swift/Xcode 9?

It might sound a trivial question but I am newbie in Swift, I was wondering whether an internal class can have public methods and variables that I would like to use from that class.

Let's say something like this( as example)

internal class myService: InheritService {

public var x : Int32 = 0

public func sum (_ a: Int32, _b: Int32) -> Int32 {

 return (a + b)

}}

Can I sue then function sum and variable x from outside?

Thanks in advance

Upvotes: 5

Views: 1983

Answers (2)

Senseful
Senseful

Reputation: 91711

Although the following code will compile without warnings, it doesn't really make sense as other comments suggest.

internal class Animal {
  public let name = "Bob"
}

Therefore, you might want to add a lint rule such as lower_acl_than_parent to your codebase to prevent you from having code like this.

Upvotes: 0

Jacob King
Jacob King

Reputation: 6157

No it can't. Any access modifiers of properties or methods contained in a first class citizen such as a class, struct, protocol or enum will always be overwritten by that of the containing definition.

Think about it this way, if you can't see a dog, how can you check how many legs it has?

struct Dog {
    public var legCount: Int = 3
}

Upvotes: 4

Related Questions