【问题标题】:NSArray from NSString (in plist format)来自 NSString 的 NSArray(以 plist 格式)
【发布时间】:2010-09-13 10:16:56
【问题描述】:

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

连接时:didRecieveData:我将最新数据添加到 NSMutableString。

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

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

谢谢

【问题讨论】:

    标签: iphone nsstring nsarray plist


    【解决方案1】:

    我在我的应用程序中做同样的事情,我使用 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 类只是一个带有一些设置器的类,用于保存分配的数据。我在这段代码中留下了一些todo ......但我希望这无论如何都能帮助你。这个对我有用。该数组是trainNumberResultSet 对象的列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多