开发者

NSArray from NSString (in plist format)

I use NSURLConnection to get the contents of a plist file from a remote server.

On connection:didRecieveData: I add the latest data to an NSMutableString.

Now my problem is adding this data to an array. So you have arrayWithContentsOfURL - which is synchronous - but i suppose I开发者_Python百科 could just add the contents of the NSString to a file in the application's documents directory and then use arrayWithContentsOfURL?

I was just hoping there might be an easier way?

Thanks


I do the same things in my apps, I use ASIHttpRequest which works perfect. After getting my string back, i translate to a dictionary.

NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:myString];

This dictionary I send to a parse class, which I can get a object from.

xmlTrainNumber* fileResult = [[[xmlTrainNumber alloc] initWithDictionary:dict] autorelease];

the class xmlTrainNumber looks like this:

#import "xmlTrainNumber.h"
#import "trainNumberResultSet.h"

@interface xmlTrainNumber (Private)

- (NSArray*)_parseXmlDictionary:(NSDictionary*)aDictionary;

@end

@implementation xmlTrainNumber

@synthesize timeXmlResult;

- (id)initWithDictionary:(NSDictionary*)aDictionary
{
    self = [super init];
    if (self)
    {
        timeXmlResult = [self _parseXmlDictionary:aDictionary];
    }
    return self;
}

- (NSArray*)_parseXmlDictionary:(NSDictionary*)aDictionary
{
    if (aDictionary != NULL && [aDictionary count] > 0)
    {
        NSNumber *version = [aDictionary objectForKey:@"version"];
        NSNumber *statusCode = [aDictionary objectForKey:@"statusCode"];

        if ([[statusCode stringValue] isEqualToString:@"1"])
        {
            NSString *title = [aDictionary objectForKey:@"title"];
            if (version != NULL)
            {
                NSArray* results = [aDictionary objectForKey:@"results"];

                if (results != NULL)
                {
                    NSMutableArray* result = [[NSMutableArray alloc] init];
                    for (NSDictionary* currentResult in results)
                    {
                        // TODO: add error handling
                        [result addObject:[[trainNumberResultSet alloc] initWithStation:[currentResult objectForKey:@"station"] 
                                                                                arrival:[currentResult objectForKey:@"arrival"] 
                                                                              departure:[currentResult objectForKey:@"departure"] 
                                                                             newArrival:[currentResult objectForKey:@"newArrival"] 
                                                                           newDeparture:[currentResult objectForKey:@"newDeparture"] 
                                                                        expectedArrival:[currentResult objectForKey:@"expectedArrival"] 
                                                                      expectedDeparture:[currentResult objectForKey:@"expectedDeparture"] 
                                                                                  track:[currentResult objectForKey:@"track"] 
                                                                                   info:[currentResult objectForKey:@"info"]
                                                                                  title:title]];
                    }

                    return result;
                }
                else
                {
                    // TODO: throw exception instead
                    return NULL;
                }
            }
            else
            {
                // TODO: throw exception instead
                return NULL;
            }
        }
        else {
            return nil;
        }

    }
    else
    {
        // TODO: throw exception instead
        return NULL;
    }
}

- (NSArray*)getTimeResult
{
    return timeXmlResult;
}

- (void)dealloc
{
    if (timeXmlResult != NULL)
    {
        [timeXmlResult release];
    }
    [super dealloc];
}

@end

The class trainNumberResultSet is just a class with some setters that saves the data assigned. I have some todo's left in this code... but I hope that this can help you anyways. It works for me. The array is a list of trainNumberResultSet objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜