简体   繁体   中英

NSMutableArray or NSArray structures

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

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.

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.

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.

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