2010-09-13 59 views
1

我使用NSURLConnection從遠程服務器獲取plist文件的內容。來自NSString的NSArray(以plist格式)

連接:didRecieveData:我將最新數據添加到NSMutableString。

現在我的問題是將這些數據添加到數組中。所以你有arrayWithContentsOfURL - 這是同步的 - 但我想我可以只添加NSString的內容到應用程序的文檔目錄中的文件,然後使用arrayWithContentsOfURL?

我只是希望有一種更簡單的方法?

感謝

回答

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只是一些制定者,節省了分配的數據類。我在這段代碼中留下了一些todo ......但我希望這可以幫助你。這個對我有用。該數組是一個trainNumberResultSet對象的列表。