0

i'm trying to determinate if the AnyClass that is send to my method is Subclass of NSManagedObject

    func myFunction (classType:AndClass){

           if classType is NSManagedObject{
              //do some stuff 
           }

    }

I guess i'm doing something wrong because the compiler doesn't accept this.

please help

Janub
  • 1,594
  • 14
  • 26

1 Answers1

1

AnyClass is a metatype. Its values are classes, not objects (instances of classes). On the other hand, the values of type NSManagedObject are objects. Thus they are not compatible. Your error probably said that "NSManagedObjcet is not a type of AnyObject.Type" (AnyClass is an alias for AnyObject.Type; the .Type signifies a metatype).

You probably wanted AnyObject instead of AnyClass.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • i had this method in objective-c - (void)myFunction:(Class)classType{ if ([class isKindOfClass:[NSManagedObject Class]]){ //do something} } , and i wanted to know how i implement it in swift – Janub Sep 21 '14 at 08:04
  • @Janub: Where does `class` come from (there is no such variable declared)? If it is the same as `classType`, then it doesn't make sense, because `[classType isKindOfClass:[NSManagedObject class]]` can never be true. – newacct Sep 21 '14 at 18:06