我需要在 swift 5 中使用两个同名的类。为此,我在两个不同的模块中创建了这两个类,但我对如何在一个中使用这两个类感到困惑 UIViewController
我的一个班级Person在models>student模块中,另一个班级Person在models模块中
我试过导入类
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
...
Run Code Online (Sandbox Code Playgroud)
但上面的 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
}
}
Run Code Online (Sandbox Code Playgroud)
而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
}
}
Run Code Online (Sandbox Code Playgroud)
我需要我的两个班级 BookViewController
我不能将班级名称更改为不同的名称,我应该使用相同的班级名称。
你可以试试 typealias:
typealias StudentPerson = yourStudentFramework.Person
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
import yourStudentFramework
class BookViewController: UIViewController {
var students:[StudentPerson] = [] //it should call yourStudentFramework.Person
var people: [Person] = [] //it should call models.Person
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1432 次 |
| 最近记录: |