0

I need help with converting one object to another. Might have searched 10-20 website didn't find any good answer.

public struct UniversityJoinChatViewModel {
    public let id: Int?
    public let name: String?
    public init(nameOfModel model : UniversityGroupChatItem?) {
          self.id = model?.id;
          self.name = model?.name;
    }
}
public struct UniversityGroupChatItem: Codable {
    public let id: Int?
    public let name: String?
    public init(id: Int?, name: String?) {
        self.id = id
        self.name = name
    }
}

I did this:

let say I have value UniversityGroupChatItem in variable universityGroupChatItem and my universityGroupChatItem contains is not nil and contains value. I tried this it did not work.

universityJoinChatViewModel = (universityGroupChatItem) as! UniversityJoinChatViewModel

The app crashed.

Then I tried:

map and compactmap

None worked.

I am not getting how to convert UniversityGroupChatItem struct to UniversityJoinChatViewModel struct.

I do not understand how to convert one struct to another struct both has same number name variables.

8
  • 2
    pardon my question, but why do you have two structs that are essentially the same? Commented Oct 7, 2020 at 11:11
  • UniversityGroupChatItem is model and UniversityJoinChatViewModel is my viewmodel I am trying to convert model to viewmodel. Commented Oct 7, 2020 at 11:58
  • Maybe using only one struct and a typealias can do the trick? Your model and ViewModel are exactly the same, I can't see the benefit of duplication here. Commented Oct 7, 2020 at 12:07
  • You already have an init that takes a UniversityGroupChatItem so you can use dependency injection but do you really want to init your view model with nil values? Anyway, I think you should add your when using map and/or compactMap so we can help you with that Commented Oct 7, 2020 at 12:31
  • @JoakimDanielson Considering that they are identical objects no need to use map, compactMap or creating any custom initializer. You can simply load its data from memory. Btw it is a single object not a collection Commented Oct 7, 2020 at 14:55

2 Answers 2

2

You can't force cast one object into another (no matter struct, class, enum ect.) even if they are fully the same inside

You need to implement inits where one object takes fields from another one.

map is function to sequences, if You have only 1 object just init it with another one

Examples:

  public struct UniversityJoinChatViewModel {
        public let id: Int?
        public let name: String?
        public init(nameOfModel model : UniversityGroupChatItem?) {
              self.id = model?.id;
              self.name = model?.name;
        }
    }
    public struct UniversityGroupChatItem: Codable {
        public let id: Int?
        public let name: String?
        public init(id: Int?, name: String?) {
            self.id = id
            self.name = name
        }
    }
    
    let universityGroupChatItem = UniversityGroupChatItem(id: 0, name: "name")
    let universityJoinChatViewModel = UniversityJoinChatViewModel(nameOfModel: universityGroupChatItem)
    let groupArray = Array(repeating: universityGroupChatItem, count: 10)
    let joinArray = groupArray.map(UniversityJoinChatViewModel.init(nameOfModel:))
Sign up to request clarification or add additional context in comments.

Comments

1

In your case, you already have the constructor that can help you to achieve what you want, so instead of trying to cast the object, create a new one:

universityJoinChatViewModel = UniversityJoinChatViewModel(nameOfModel: universityGroupChatItem)

1 Comment

Thank you so much for the answer. I confused myself converting an array with converting an struct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.