1

I’m trying out Swift playgrounds and I cannot find a way to change value of a struct. Below I want to change properties of Shadow from default values.

I’ve tried the initialiser and dot syntax but I get ‘field is inaccessible due to internal protection level.

let circle = Circle()
circle.draggable = true
//var shadow = Shadow(color: #colorLiteral(red: 0.9529411764705882, green: 0.6862745098039216, blue: 0.13333333333333333, alpha: 1.0), offset: Point(3,-3), blurRadius: 5, opacity: 1)
var shadow = Shadow()
shadow.color = .red
circle.dropShadow = shadow
user14492
  • 2,116
  • 3
  • 25
  • 44

1 Answers1

3

You’re apparently using the “Shapes” playground.

So, view the source by clicking on “...” » “Advanced” » “View Auxiliary Source Files” » “Contents” » “Modules” » “Book.playgroundmodule” » “Sources” » “PlaygroundAPI”.

If you look at the Shadow struct, none of those properties are declared as public. That means that you don’t have access to them outside of that module.

In contrast, if you look at the Circle declaration in contrast, radius is public. And if you look at AbstractDrawable, dropShadow is public, too.

In the absence of an explicit access qualifier (e.g., public), a property gets the internal access qualifier, only accessible within that module. (See The Swift Programming Language: Access Control.) And your code in that playground is not within the same module as where Shadow was defined. Thus you don’t have access to it.

So, bottom line, your warning is just telling you that you cannot access this internal property of the Shadow struct.


This begs the question as to why they declared Shadow such that you can’t customize the nature of the shadow. I suspect it is just an oversight on their part. For example, I opened up this playground workbook in Xcode and replaced the init method for Shadow with the following:

public init(offset: Point = Point(x: 1, y: -1), blurRadius: Double = 1, opacity: Double = 0.3, color: Color = .black) {
    self.offset = offset
    self.blurRadius = blurRadius
    self.opacity = opacity
    self.color = color
}

Then I could reopen this playground on my iPad and do things like:

let circle = Circle(radius: 30)
circle.dropShadow = Shadow(opacity: 0.9, color: .green)

And that yielded:

big circle green shadow

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Hmm.. wow. Can't believe they would mess up something so obvious. Should this be reported to "Apple - The Most Innovative Privacy-Concerned Company on the Planet"? About viewing the source, is that on Xcode on PC? Bcz I was working on the iPad and don't know how to follow that. I assume the same for "the playground workbook"; you can only edit that on PC? – user14492 Jan 30 '20 at 10:25
  • 1
    You can *view* the source right on the iPad (by tapping the “...” button and the subsequent menu items shown in my answer above). But to *edit* the extra source files buried within that playground, I had to do that on my Mac, opening up the the playground sitting in my iCloud Drive (when I create playgrounds on the iPad, they’re saved in iCloud, which I can then open in macOS). – Rob Jan 30 '20 at 14:24