[英]same class name in different modules
I need to use two classes with the same name in swift 5. For this, I have created those two classes in two different modules, but I am confused on how to use both the classes in an UIViewController
我需要在 swift 5 中使用两个同名的类。为此,我在两个不同的模块中创建了这两个类,但我对如何在UIViewController
中使用这两个类感到困惑
one of my class is Person
which is in models
> student
module and another class is Person
with is in the models
module我的Person
之一是models
> student
模块中的Person
,另一个 class 是models
模块中的人
I have tried importing class like我试过导入 class 之类的
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
...
but above Person class is pointing to models.Person
only, It is not pointing to models.student.Person
但上面的 Person class 仅指向models.Person
,它不指向models.student.Person
Person class in models > Person.swift is型号中的人 class > 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
}
}
and the models.student.Person.swift is和 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
}
}
I need both the class in my BookViewController
我的 BookViewController 中同时需要BookViewController
I can't change the name of the class to a different one, I should use the same class name.我无法将 class 的名称更改为其他名称,我应该使用相同的 class 名称。
You could try typealias:你可以试试 typealias:
typealias StudentPerson = yourStudentFramework.Person
and then use it like:然后像这样使用它:
import yourStudentFramework
class BookViewController: UIViewController {
var students:[StudentPerson] = [] //it should call yourStudentFramework.Person
var people: [Person] = [] //it should call models.Person
...
You should use from Protocol Oriented for this situation对于这种情况,您应该使用 from Protocol Oriented
Visit https://medium.com/nsistanbul/protocol-oriented-programming-in-swift-ad4a647daae2访问https://medium.com/nsistanbul/protocol-oriented-programming-in-swift-ad4a647daae2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.