Objective-C: How to change the class of an object at runtime?

后端 未结 2 1716
庸人自扰
庸人自扰 2020-12-01 11:57

I tried to answer Using a UITableView subclass with a UITableViewController with ISA Switching like so:

self.tableView->isa = [MyTableView class];
         


        
2条回答
  •  自闭症患者
    2020-12-01 11:58

    The safe way is to create a new instance.

    Swapping isa is not safe - you have no idea what the memory layout of a class is or what it will be in the future. Even moving up the inheritance graph is really not safe because objects' initialization and destruction would not be performed correctly - leaving your object in a potentially invalid state (which could bring your whole program down).

提交回复
热议问题