简体   繁体   中英

Difference between CFMutableArray and NSMutableArray?

What is the difference between NSMutableArray and CFMutableArray ?

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

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 .

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.

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 eg Cocoa without having to constantly convert types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM