You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 11:11:02