Managing tuple sets in Objective-C

I am new to Objective-C and wondered what the best way to manage tuple collections was. In C, I would use a 2D array or structure.

Should I create objects containing these tuples? Does it seem that there is an excess for just sorting the lists or is there no real additional load generated by the initialization of the object?

+6
collections objective-c cocoa tuples
source share
3 answers

There are definitely some overheads in generating objects. For a small number of objects, then using ObjC data structures is still suitable. If you have a large number of tuples, I would handle them in an array of C structures. Remember that Objective-C is actually just C. It is appropriate and common to use C constructors in Objective-C (to the point; learning where this point represents an important milestone in becoming a good Objective-C developer).

Usually for this type of data structure, I probably create a single Objective-C object that manages the entire collection. This way, external subscribers will see the Objective-C interface, but internal data will be stored in a more efficient C structure.

If you often access many tuples, my collection object will probably provide get methods similar to [NSArray getObjects:range:] . ObjC methods starting with "get" indicate that the first parameter is a pointer to be overwritten by this method. This is commonly used for high-performance C access to things controlled by an ObjC object.

This data structure is just like ObjC developers combine ObjC's elegance and maintainability with C performance and simplicity.

+12
source share

I think you will have to settle for NSArray NSArray objects, or perhaps NSArray NSDictionary objects. You can always roll your own class or do it the way you would in C.

+1
source share

There are several ways you could do this:

  • CoreData Although this is not a technical database, it can behave as one. If you do not require consistency between application launches, consider using the NSInMemoryStoreType storage type, as opposed to NSSQLiteStoreType or another parameter. However, if you want to join tuples together, using CoreData absolutely does not work (this, IMO, is the main reason why CoreData is not a database).
  • Use a real database. SQLite ships on all Macs and iPhones and is pretty easy to use if you use wrappers like FMDB or SQLite Persistent Objects or PLDatabase or EGODatabase or Google's GTMSQLite wrapper .
  • A tuple is just a collection of key-value pairs, so you can just use NSMutableArray from NSMutableDictionaries. Obviously, you will not be able to use SQL syntax and any connections / queries that you must run yourself, but this will certainly have the easiest setup.
  • Write a tuple class and store it in an NSMutableArray (similar to # 3, just applying a common set of attributes in your tuples).
+1
source share

All Articles