Reputation: 1453
What is the difference between NSMutableArray
and CFMutableArray
?
In which case(s) should we use one or the other?
Upvotes: 3
Views: 2666
Reputation: 1054
OSX's APIs are layered, there are basic low-level APIs that are self-cotnained and then there are richer APIs built on top of them, in turn using the lower level APIs themselves.
CFMutableArray is part of the CoreFoundation framework and used by the lower-level APIs. NSMutableArray (I guess NS stands for NextStep) is part of the Foundation framework and used in higher level APIs like the AppKit or Cocoa.
Which you should use depends on where you are working. If you're working in a rich user interface using Cocoa, NSMutableArray is the right choice. If you're working on a daemon, driver or anything else just using CoreFoundation, use CFMutableArray.
Luckily, as pointed out above, many of these CF/NS types are toll-free bridged and so you can use CoreFoundation APIs from e.g. Cocoa without having to constantly convert types.
Upvotes: 1
Reputation: 104698
At runtime, they are identical. They are the same, they are toll-free bridged types - you can safely (and efficiently) cast one to the other.
They are different types, available in different/overlapping languages.
CFMutableArrayRef is the opaque C object interface NSMutableArray * is the Objective-C interface
They may be freely interchanged, and the difference is the declaration that says one is a pointer to an opaque type, vs a objc object.
Also, you can (sorta - it requires a little more implementation than usual) subclass NSMutableArray type using objc.
Upvotes: 1
Reputation: 35925
CFMutableArray
and NSMutableArray
are the C- and Objective-C-equivalents of the same type. They are considered a "toll free bridged" type pair, which means you can cast a CFMutableArrayRef
to a NSMutableArray*
(and vice versa) and everything will just work. The cases in which you would use one over the other is ease-of-use (are you using C or Objective-C?) or compatibility with an API you would like to use. See more information here.
Upvotes: 14