Reputation: 79
I just migrated my Swift 1.2 code to Swift 2 code and I got the error
value of type "
UIViewController
" has no member 'className
'
on this line:
if(childViewController.className=="myPhotoCalendar.GalleryController")
Here is my code :
func initLeftMenuController()
{
for childViewController in self.childViewControllers
{
if(childViewController.className=="myPhotoCalendar.GalleryController")
{
self.galleryController=childViewController as! GalleryController
}
if(childViewController.className=="myPhotoCalendar.TextsController")
{
self.textsController=childViewController as! TextsController
}
if(childViewController.className=="myPhotoCalendar.TemplatesController")
{
self.templatesController=childViewController as! TemplatesController
}
if(childViewController.className=="myPhotoCalendar.StylesController")
{
self.stylesController=childViewController as! StylesController
}
if(childViewController.className=="myPhotoCalendar.ObjektsController")
{
self.objektsController=childViewController as! ObjektsController
}
}
}
Anyone have an idea of the equivalent of className
in Swift 2 ?
Thanks for your help.
Upvotes: 1
Views: 342
Reputation: 57124
Checking for class name similarity is not a good idea at all - use if let
instead. What would you do if you change the name of a class? That might not be covered via refactoring and the app would stop working.
The best solution therefore is not looking for an alternative for className
but use the better and more "swifty" way - something like
if let ctrl = childViewController as? GalleryController {
self.galleryController = ctrl
} else if (...) {
...
}
Or use a switch statement (as Martin stated) (which I did not know was possible until looking it up):
switch childViewController {
case let ctrl as GalleryController:
self.galleryController = ctrl
case let ctrl as SomeOtherClass:
self.something = ctrl
// more cases
default:
break
}
Upvotes: 1