使用TipKit遇「从非协议类型Tip继承」错误,求解决方法
解决TipKit中“Inheritance from non-protocol type 'Tip'”报错
这个报错的核心原因是:TipKit框架中的Tip是一个类,而非协议,而Swift的结构体(struct)不支持继承类,只能遵循协议,因此用struct尝试继承Tip类会触发该错误。
修正方案
将定义从struct改为class,并确保属性符合Tip基类的要求:
import TipKit import SwiftUI class FavoriteBackyardTip: Tip { override var title: Text { Text("Save as a Favorite").foregroundColor(.indigo) } override var message: Text? { Text("Your favorite backyards always appear at the top of the list.") } override var asset: Image? { Image(systemName: "star") } }
额外说明
- 必须使用
class继承Tip基类,因为Tip是抽象类,设计用于被子类继承扩展。 Tip基类中的message和asset属性默认是可选类型(Text?、Image?),你可以选择返回具体值(如示例中)或者nil。- 需要导入
SwiftUI,因为Text和Image属于SwiftUI框架。
内容的提问来源于stack exchange,提问作者user14367248
相关产品推荐
相关产品推荐

