views:

240

answers:

4

What is the difference between NSMutableArray and CFMutableArray?

In which case(s) should we use one or the other?

A: 

You can get idea from this
http://stackoverflow.com/questions/1539017/nsarray-with-c-types

Brij
+8  A: 

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.

fbrereto
+1  A: 

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.

Justin
+1  A: 

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.

Adrian