gpt4 book ai didi

swift - 如何访问实现两个协议(protocol)的一个 Struct 属性?

转载 作者:行者123 更新时间:2023-11-30 11:25:03 26 4
gpt4 key购买 nike

我刚刚学习 Swift 4,在尝试访问必须实现 2 个协议(protocol)的结构体的属性时遇到了一些麻烦

这是我的结构

struct FigureA {
static func load() -> Model {
return Model.make(
name: "FigureName",
status: "Painted",
image: UIImage(named: "FigureA"),
description: "Good figure")
}
}

这里是协议(protocol) 1

protocol ListModel: class {
var name: String { get }
var status: String { get }
var image: UIImage? { get }
}

这里是协议(protocol)2

protocol DetailModel: ListModel {
var categoryName: String { get }
var modelDescription: String? { get }
}

我想访问该结构的描述,但我根本不知道如何实现。

有人能给我一点启发吗?

最佳答案

这是您的良好开端:

protocol BaseProtocol {
var id: Int { get set }
}

protocol PersonProtocol: BaseProtocol {
var firstName: String { get set }
var lastName: String { get set }
var name: String { get }
}

struct Person: PersonProtocol {
var id: Int
var firstName: String
var lastName: String
var name: String { return firstName + " " + lastName }
}

//≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

//Create Struct Instance & Print properties.
let person = Person(id: 1001, firstName: "Manish", lastName: "Rathi")
print(person.id)
print(person.firstName)
print(person.lastName)
print(person.name)
}
}

#HappyCoding😊

关于swift - 如何访问实现两个协议(protocol)的一个 Struct 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50833824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com