1

In NSMutableArray or NSArray how do you set up the row structure. All the examples I can find just deal with one dimensional integers or strings.

So how do you go about setting up an array row structure like:

Integer, String, Date

1
  • 1
    Make a custom class defines your fields and then use the array of those objects. I think that can help you. Commented Jul 6, 2012 at 10:19

3 Answers 3

3

You would need to create an array of arrays, or an array of dictionaries, the second approach being better if you'd like to refer to the data by name rather than index:

NSMutableArray *rows = [[NSMutableArray alloc] init];
NSMutableDictionary *firstRow = [NSMutableDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt:12], @"fieldWithNumber",
    [NSString stringWithFormat:@"A formatted string %ld", 34], @"fieldWithString",
    [NSDate date], @"fieldWithDate",
    nil];
[rows addObject:firstRow];
// etc.
Sign up to request clarification or add additional context in comments.

Comments

2

You can make custom class and add collection of these objects in an array.

For example please assume that we have to do some rows about places then create a class PLACE.h

@interface PLACE : NSObject 
{
   NSString *title;
   NSString *summary;
   NSString *url;
   NSString *latitude;
   NSString *longitude;
}

@property   (nonatomic, retain) NSString    *title;
@property   (nonatomic, retain) NSString    *summary;
@property   (nonatomic, retain) NSString    *url;
@property   (nonatomic, retain) NSString    *latitude;
@property   (nonatomic, retain) NSString    *longitude;

@end

PLACE.m

@implementation PLACE

@synthesize title;
@synthesize summary;
@synthesize url;
@synthesize latitude;
@synthesize longitude;


- (void) dealloc
{
  [title release];
  [summary release];
  [url release];
  [latitude release];
  [longitude release];
  [super dealloc];
}
@end

Create instance of the class PLACE for each place and hold the objects in the array. I think that solves your problem.

2 Comments

Just so I have this right using this approach the array is called PLACE after the object and its structure and if using ARC I ignore the release/super dealloc statements?
please do that if you are using ARC.
-1

An array is just a collection of objects.

Just create an object that has an integer, date and string and then add instances of those to the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.