来自NSString的NSArray(以plist格式)

时间:2010-09-13 10:16:56

标签: iphone nsstring nsarray plist

我使用NSURLConnection从远程服务器获取plist文件的内容。

On connection:didRecieveData:我将最新数据添加到NSMutableString。

现在我的问题是将此数据添加到数组中。所以你有arrayWithContentsOfURL - 这是同步的 - 但我想我可以只将NSString的内容添加到应用程序文档目录中的文件中,然后使用arrayWithContentsOfURL?

我只是希望有一种更简单的方法?

由于

1 个答案:

答案 0 :(得分:0)

我在我的应用中做同样的事情,我使用ASIHttpRequest,这是完美的。收到我的字符串后,我翻译成字典。

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

这个字典我发送到一个解析类,我可以从中获取一个对象。

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

类xmlTrainNumber如下所示:

#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

trainNumberResultSet只是一个包含一些保存所分配数据的setter的类。我在这段代码中留下了一些todo ...但我希望这可以帮助你。这个对我有用。该数组是trainNumberResultSet个对象的列表。