[英]NSArray from NSString (in plist format)
I use NSURLConnection to get the contents of a plist file from a remote server. 我使用NSURLConnection从远程服务器获取plist文件的内容。
On connection:didRecieveData: I add the latest data to an NSMutableString. 在connection:didRecieveData上:我将最新数据添加到NSMutableString中。
Now my problem is adding this data to an array. 现在我的问题是将此数据添加到数组中。 So you have arrayWithContentsOfURL - which is synchronous - but i suppose I could just add the contents of the NSString to a file in the application's documents directory and then use arrayWithContentsOfURL?
因此,您拥有arrayWithContentsOfURL-是同步的-但我想我可以将NSString的内容添加到应用程序文档目录中的文件中,然后使用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. 我在我的应用程序中做同样的事情,我使用了ASIHttpRequest ,效果很好。 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: 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
The class trainNumberResultSet
is just a class with some setters that saves the data assigned. trainNumberResultSet
类只是一个带有一些设置器的类,用于保存分配的数据。 I have some todo
's left in this code... but I hope that this can help you anyways. 我在这段代码中还剩下一些
todo
...但是我希望无论如何都能对您有所帮助。 It works for me. 这个对我有用。 The array is a list of
trainNumberResultSet
objects. 该数组是
trainNumberResultSet
对象的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.