7

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Dhanu K
  • 11,288
  • 6
  • 24
  • 38
  • 1
    Possible duplicate of [How to use Namespaces in Swift?](https://stackoverflow.com/questions/24002821/how-to-use-namespaces-in-swift) – TylerP Oct 26 '19 at 04:49
  • I was the second to vote to close. This isn't something normally needed - as in, frameworks should do their best to insure this doesn't happen. To quote the **only** thing that matters in your question... *"I need to use two classes with the same name...."* If subclassing or creating two instance of a class don't work, then something else is wrong, and your sample code really doesn't help to duplicate it. It *may* be a Swift issue, but what you've posted (a) sounds like more than that (OOP?) but (b) isn't at all duplicatable. Could you provide more code? It would help. –  Oct 26 '19 at 05:09
  • @dfd I have updated my question more specifically regarding my problem – Dhanu K Oct 26 '19 at 05:24
  • I mean this in the kindest way possible, but this setup simply cannot work. This is the *base* reason for using namespaces - avoiding conflicts like this. A "student" should *always* be a "person", but a "person" *may* be a student. Can you at least have Student inherit (or extend) from Person? –  Oct 26 '19 at 11:44
  • @dfd, No I am sending an example only, my actual requirement is max all parameters in the class are different from eachother – Dhanu K Oct 26 '19 at 11:52

1 Answers1

11

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
...
C. Kontos
  • 1,198
  • 12
  • 21