Is there any way to monkey-patch or swizzle an NSArray (or other Class Cluster)?

后端 未结 2 1972
梦谈多话
梦谈多话 2021-02-10 16:02

Today I was working on a project in which I wanted to \"alias\" an alternative method for all instances of NSArray, and didn\'t think it would be too difficult with

2条回答
  •  孤城傲影
    2021-02-10 16:43

    It's not just that it's a class cluster. NSArray is toll-free bridged to CFArray, and you can't swizzle Core Foundation. So this is very unlikely to work in general.

    But what are you trying to solve? If you want to add a new method, use a category. They work on class clusters just fine. Modifying the behavior of some built-in on NSArray seems a recipe for disaster (entertaining as it might be as an exercise).

    Before going too far, you probably want to at least take a look at CFArray.c and understand how some of the underlying stuff is implemented.


    EDIT: While I would never do this in production code, you may get some of what you want by hijacking individual array instances with ISA-swizzling. See ISASwizzle for some example code. The code explanation is in Chapter 20 of iOS:PTL. Search out for "isa swizzle" and you should find more on the net. It's how KVO is implemented. But with NSArray... wow, that's gotta be fragile.

提交回复
热议问题