Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

same class name in different modules

Tags:

ios

swift

swift5

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

one of my class is Person which is in models > student module and another class is Person with is in the models module

I have tried importing class like

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 in models > Person.swift is

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

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

I can't change the name of the class to a different one, I should use the same class name.

like image 877
Dhanu K Avatar asked Jul 20 '26 13:07

Dhanu K


1 Answers

You could try 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
...
like image 137
C. Kontos Avatar answered Jul 23 '26 05:07

C. Kontos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!