silikonopolis.blogg.se

Swift downcast
Swift downcast









swift downcast

The forced form, as!, attempts the downcast and force-unwraps the result as a single compound action.

swift downcast

The conditional form, as?, returns an optional value of the type you are trying to downcast to. If we try to get any content from this array, it will return you an object of type LivingBeing.In that case we can try to downcast it after fetching it form the array. Since array contains one Animal object and one Human object, the type inference will decide the array type as LivingBeing. We know that the first item is of type Animal. Where you believe this is the case, you can try to downcast to the subclass type with a type cast operator ( as? or as!). for item in livingBeingArray DowncastingĪpple doc says: A constant or variable of a certain class type may actually refer to an instance of a subclass behind the scenes. Let’s iterate the array objects over a for loop. The type check operator returns true if the instance is of that subclass type and false if it is not.Ĭonsider the following code: let livingBeingObj = livingBeingArray // returns a LivingBeing object. Use the type check operator ( is) to check whether an instance is of a certain subclass type. In order to work with them as their native type, you need to check their type, or downcast them to a different type. However, if you iterate over the contents of this array, the items you receive back are typed as LivingBeing, and not as Human or Animal. The items stored in livingBeingArray are still Human and Animal instances behind the scenes. Swift’s type checker is able to deduce that Human and Animal have a common superclass of LivingBeing, and so it infers a type of for the livingBeingArray array.

swift downcast

What do you think the type of this array created via type inference? It will be of type. My previous answer stated to use the decorator, which also works, but the above solution is documented in the swift / cocoa reference.Now create a constant array called livingBeingArray with one Animal class object and one human class object. I consider this a bug in the current beta, and have raised it as such. This has to be done after you generate the class file, otherwise that part fails. You have to add the module or app name to the class name in the model editor: MyApp.Task However, you can cast the objects in your tasks array when you need them: tasks = managedObjectContext.executeFetchRequest(fetchRequest, error: &error) // array of NSManagedObjects Objects of a class type, such as NSObject or NSArray, can now be downcast to bridged Swift What am I doing wrong?Answer1:Īccording to the XCode Release Notes for Beta 4 the downcast should work now: If I replace the assignment line with var temp = managedObjectContext.executeFetchRequest(fetchRequest, error: &error) as, the error does not occur, but I need to save it as an array of Tasks in order to be able to use the custom classes that I generated. However, when I run the the application, I get the following runtime error at the line where I assign the array to tasks: fatal error: array cannot be downcast to array of derived Tasks = managedObjectContext.executeFetchRequest(fetchRequest, error: &error) as Įarlier in the view controller, I initialized tasks: var tasks : = Let entity = NSEntityDescription.entityForName("Task", inManagedObjectContext: managedObjectContext) In my view controller, in view did load, I have the following code.

swift downcast

I auto generated the classes for the two entities:Ĭlass Task: NSManagedObject var name: var due: var subject: Subject I have two entities in my data model: Task and Homework. I am using Core Data in Swift and am having trouble storing the array returned from a fetch request. How come I can cast to NSManagedObject but not to my entity's type? This question already has an answer here:











Swift downcast