English 中文(简体)
NSArray s, Primitive types and Boxing Oh My!
原标题:

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits.

Now here s the question: I feel really inclined to create some type of simple Objective-C collection to hold primitive types because I do not like the whole concept of wrapping a primitive type into an object (namely NSNumber) and then adding that to a collection such as NSArray.

The thing is, is that I completely understand that NSArray s only operate on objects. However it just seams really dumb to me that we are forced to work with boxed primitives just because no collection exists to work on primitive Integers for instance.

So the C# guys will know what I m talking about in the old days with .net. Generally you d either use a simple array or an ArrayList back when generics weren t around yet. The ArrayList essentially forced you to box your primitive types but it actually did it behind the scenes and you paid a dear cost for it (in terms of performance.)

If you couldn t deal with the performance cost then you rolled your own custom collection or eventually used generics when they came out.

Now in the Objective-C world it seams we are forced to deal with this problem again but it s like the Apple developers don t really care, or it s just not a big deal or what.

So my question is: What is the recommend practice here? Should I try to build my own custom Objective-C collections for holding an NSInteger for example? Or should I just use a native C array as fellow coder Memo suggests in this blog post: NSArray vs. C Array performance

Or do I just stick with using Apple s built-in collections and bite the bullet. It seams that the performance characteristics explained by Memo are worth considering when building performance critical apps.

This post was a mouthful but I had to get it off my chest.

最佳回答

Personally I d suggest using objective C++ and std::vector<>; unless you really have a need to be using a native objective C container, in which cause you re stuck with using objects.

问题回答

If you want to hold only NSIntegers, can you just use a regular C array?

Edit: If you re doing this for performance reasons, it sounds like a whole lot of premature optimization to me.

I’ve never actually needed a dynamic array of integers as there’s always been a better solution for the problem I’m trying to solve. For example, NSData can store an array of bytes, or you can use NSIndexSet if it’s a set of integers that you want.

You can also use CFArrayCreate and pass in appropriate callback functions (and wrap it in Objective-C if you wanted to).

Do you have a specific problem that you’re trying to solve?

Going the C array way via malloc requires consideration the array owner s life cycle in order to free it. Other considerations e.g. ARC and resizing the array, etc. make the wrapping approach appealing. The only reasons I see for C style is if the project is a port or based on profiler results. If you decide to wrap consider an approach like NSArray+Primitive that creates a category to add primitive type functionality to NSArray and NSMutableArray.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签