swiftui - an instance calls a static method? - Stack Overflow

admin2025-05-01  2

The following SwiftUI code works in Swift Playgrounds:

Circle()
    .fill(.blue.shadow(.drop(radius: 10)))

but I can't figure out why it really works, here are my questions:

  1. .blue is an instance of type Color, right?
  2. why can it call the ShapeStyle.shadow() method, isn't it a static method?

I have chatted with ChatGPT, Gemini, Claude, Perplexity, but no one can give me a reasonable explanation, please help.

The following SwiftUI code works in Swift Playgrounds:

Circle()
    .fill(.blue.shadow(.drop(radius: 10)))

but I can't figure out why it really works, here are my questions:

  1. .blue is an instance of type Color, right?
  2. why can it call the ShapeStyle.shadow() method, isn't it a static method?

I have chatted with ChatGPT, Gemini, Claude, Perplexity, but no one can give me a reasonable explanation, please help.

Share Improve this question asked Jan 2 at 21:54 Pegasus RoePegasus Roe 192 bronze badges 2
  • 2 It's an instance method, not a static function. See shadow(_:) – Benzy Neez Commented Jan 2 at 22:03
  • Thanks a lot, I thought there was just a static shadow(_:) method. – Pegasus Roe Commented Jan 3 at 0:19
Add a comment  | 

1 Answer 1

Reset to default 0

Thanks to @BenzyNeez, now I know there are TWO .shadow() methods in ShapeStyle:

  • type method: .shadow()
  • instance method: .shadow()
转载请注明原文地址:http://anycun.com/QandA/1746095126a91596.html