Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

swift - 在Swift中,用于创建给定基类的未指定子类的“包装”子类的语法是什么?(In Swift, what's the syntax for making a “wrapper” subclass of an unspecified subclass of a given base class?)

This one is driving me nuts since I feel it should be possible, but I just cannot make the compiler happy no matter how I try to express the idea...

(因为我认为这是可能的,所以这让我发疯了,但是无论我如何尝试表达这个想法,我都无法使编译器感到高兴...)

I'm working on a SpriteKit project.

(我正在做一个SpriteKit项目。)

One of the classes is a wrapper around SKSpriteNode to add an action that the sprite calls when it's touched:

(其中一个类是SKSpriteNode的包装器,用于添加一个动作,该动作在精灵被触摸时调用:)

class TouchableSprite: SKSpriteNode {
  /// The action to run upon touch
  var action: (() -> Void)? = nil

  /// Make the sprite react to touches
  override var isUserInteractionEnabled: Bool {
    get { return true }
    set {}
  }

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    action?()
  }
}

All well and good.

(一切都很好。)

Now I want to have an SKLabelNode with the same functionality.

(现在,我想拥有一个具有相同功能的SKLabelNode。)

Just duplicating the code but with SKLabelNode works, but I figured I should be able to have a generic wrapper that would work for any of the various SKNode subclasses.

(只是复制了代码,但是使用SKLabelNode可以工作,但是我认为我应该能够有一个通用包装器,该包装器适用于各种SKNode子类中的任何子类。)

So...

(所以...)

class Touchable<T>: T where T: SKNode { ...

Nope, Inheritance from non-protocol, non-class type 'T'.

(不,从非协议,非类类型“ T”继承。)

class Touchable<T: SKNode>: T { ...

Same deal... Oh, I know!

(同样的交易...哦,我知道!)

class Touchable: T where T: SKNode { ...

At least that's a different error: 'where' clause cannot be attached to a non-generic declaration.

(至少这是一个不同的错误:'where'子句不能附加到非泛型声明中。)

class Touchable<T>: T where <T: SKNode> { ...

No, Closure expression is unused.

(否,闭包表达式未使用。)

After what feels like a zillion different tries, I feel like the world's biggest idiot for having to ask how to write this, but I really want to know.

(在经历了无数次尝试之后,我觉得自己是世界上最大的白痴,不得不问如何写这篇文章,但我真的很想知道。)

Oh, I also thought maybe there could something odd about SpriteKit's class hierarchy (because of the dual Swift/Objective C support), so I also tried various permutations with just a pure Swift class.

(哦,我还认为SpriteKit的类层次结构可能有些奇怪(由于对Swift / Objective C的双重支持),所以我还尝试了一个纯Swift类的各种排列。)

class Base {
}

class Wrapper<T: Base>: T {
}

Same array of messages;

(相同的消息数组;)

I just can't get this right.

(我就是做错了。)

Can someone please tell me how to express this idea, or at least tell me why it's not possible.

(有人可以告诉我如何表达这个想法,或者至少告诉我为什么这不可能。)

  ask by bg2b translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...