gpt4 book ai didi

ios - 不同模块中的相同类名

转载 作者:行者123 更新时间:2023-12-04 01:38:21 26 4
gpt4 key购买 nike

我需要在 swift 5 中使用两个具有相同名称的类。为此,我在两个不同的模块中创建了这两个类,但我对如何在 UIViewController 中使用这两个类感到困惑。

我的一个类是Person位于 models > student模块和另一个类是Personmodels 中模块

我试过导入类

import class models.student.Person

class BookViewController: UIViewController {
var students:[Person] = [] //it should call models.student.Person
var people: [Person] = [] //it should call models.Person
...

但上面的 Person 类指向 models.Person只是,它不指向 models.student.Person
模型中的 Person 类 > Person.swift 是
import Foundation

// MARK: - Person
public struct Person: Codable {
public let firstName: String?
public let lastName: String?
public let address: String?
public let phone: String?

enum CodingKeys: String, CodingKey {
case firstName = "first_name"
case lastName = "last_name"
case address = "address"
case phone = "phone"
}

public init(firstName: String?, lastName: String?, address: String?, phone: String?) {
self.firstName = firstName
self.lastName = lastName
self.address = address
self.phone = phone
}
}

和 models.student.Person.swift 是
import Foundation

// MARK: - Person
public struct Person: Codable {
public let fullName: String?
public let educationalQualification: String?
public let college: String?

enum CodingKeys: String, CodingKey {
case fullName = "full_name"
case educationalQualification = "educational_qualification"
case college = "college"
}

public init(fullName: String?, educationalQualification: String?, college: String?) {
self.fullName = fullName
self.educationalQualification = educationalQualification
self.college = college
}
}

我的 BookViewController 中的两个类(class)都需要

我不能将类名更改为不同的,我应该使用相同的类名。

最佳答案

你可以试试 typealias:

typealias StudentPerson = yourStudentFramework.Person
然后像这样使用它:
import yourStudentFramework

class BookViewController: UIViewController {
var students:[StudentPerson] = [] //it should call yourStudentFramework.Person
var people: [Person] = [] //it should call models.Person
...

关于ios - 不同模块中的相同类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58568112/

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