-6

Im trying to get class from string in Swift. It's not work. I try following:

print(NSClassFromString(dictionary[key]!) ?? "error")

It produce "error". However, in log i can see

po dictionary[key]!
"VacanciesCell"

Why is that happening?

Vacancies cell defined as:

import UIKit

class VacanciesCell: BaseCell, ModelBinding {

...

}
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107

1 Answers1

1

Problem solved by following:

for (key, _) in dictionary {
    print(NSClassFromString(dictionary[key]!) ?? "error")
    print("key - \(key) val \(dictionary[key]!)")
    let cellClassStr = "AppName.\(dictionary[key]!)"

    self.register(NSClassFromString(cellClassStr), forCellReuseIdentifier: key)
}

Where "AppName" is app name.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107