问题描述:

There are three new private class added is iOS 10

  • __NSSingleObjectArrayI
  • __NSSingleObjectSetI
  • __NSSingleEntryDictionaryI

When container init with single object, the cluster of container will use these three new class.

In iOS 9, '__NSArray0' and '__NSDictionary0' added too.

I don't know the reason to add these special private class. Can someone tell me why?

网友答案:

It's just an optimisation most likely. For example, __NSSingleObjectSetI doesn't need to bother with an actual hash table, because there's only one object to worry about. Methods like containsObject: don't need to loop over anything or look anything up, it can simply compare the argument with the single object that the set/array/dictionary represents.

Similarly, __NSArray0 (assuming it is used for empty arrays) can have methods like arrayByAddingObjectsFromArray: simply send copy to the argument and return it. A minor optimisation, but an optimisation nonetheless.

You shouldn't use these classes directly of course (since they are private, and an implementation detail), the framework will use these classes automatically. Since it is an implementation detail, I can't be certain of the true purposes of these private classes, I'm only guessing.

相关阅读:
Top