开发者社区 问答 正文

swift 类中使用泛型报错 <b> ?报错

小弟刚刚学swift些了一段代码但是运行时报错感觉很迷惑,希望有人可以解惑什么地方错了:

 class Pushpop<T> {
    var items = [T]()
    func push(item:T) {
        items.append(item)
    }
    func pop() {
        items.removeLast()
    }

}
var stack = Pushpop<String>()
stack.push("things")
stack.push("other")
stack.pop()
stack.push("ss")
for item in stack.items {
    print(item)
}

图片说明

展开
收起
爱吃鱼的程序员 2020-06-23 14:19:35 347 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    你在哪里运行的?我在playground上跑了一遍,无误。

    xcode中的CommandLineTool工程

    没有错误啊,这段代码!

    2020-06-23 14:19:54
    赞同 展开评论
问答分类:
问答地址: