简体   繁体   English

NSMutableDictionary的NSMutableData Plist

[英]NSMutableData Plist for NSMutableDictionary

I am loading a plist via NSURLConnection into NSMutableData. 我正在通过NSURLConnection将plist加载到NSMutableData中。 After that is done I want to read the PLIST into a NSMutableDictionary. 完成之后,我想将PLIST读入NSMutableDictionary。 And then add the objects into my array to display them in a tableview. 然后将对象添加到我的数组中以在表视图中显示它们。 But at the moment I don't know how to extract the data from NSMutableData into my NSMutableDictionary. 但是目前我还不知道如何从NSMutableData中提取数据到我的NSMutableDictionary中。 If I save the data local as plist on the iPhone in some folder and then read the plist into my Dictionary it works. 如果我将本地数据作为plist保存在iPhone上的某个文件夹中,然后将plist读入我的Dictionary中,它将起作用。 But isn't there a way to do this directly? 但是没有办法直接做到这一点吗?

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    receivedData = [[NSMutableData alloc] initWithData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSData *data = [[NSMutableData alloc] initWithData:receivedData];

    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];


    NSDictionary *myDictionary = [unarchiver decodeObjectForKey:@"Beverage"];

    [unarchiver finishDecoding];

    beverageArray = [[NSMutableArray alloc] init];

    beverageArray = [myDictionary objectForKey:@"Beverage"];

    NSLog(@"%@", beverageArray);

}

Before using NSURLConnection I used this which works: 在使用NSURLConnection之前,我使用了此方法:

- (void) makeDataBeverage {

    beverageArray = [[NSMutableArray alloc] init];

    NSMutableDictionary *beverageDic = [[NSMutableDictionary alloc]initWithContentsOfURL:[NSURL URLWithString:NSLocalizedString(@"beverage", nil)]];

    beverageArray = [beverageDic objectForKey:@"Beverage"];

Now I want to the same with using NSURLConnection. 现在我想使用NSURLConnection一样。

Assuming you have the complete data(*), you'll want to look into the NSPropertyListSerialization class. 假设您具有完整的数据(*),则需要研究NSPropertyListSerialization类。 Its +propertyListWithData:options:format:error: method should get you what you're looking for, and you can use the options parameter to get the results as a mutable dictionary or array. 它的+propertyListWithData:options:format:error:方法应该可以为您提供所需的信息,并且可以使用options参数将结果作为可变字典或数组来获取。

(*)It sounds like you have the complete data, since you say you can write it to a file and then read it in using dictionaryWithContentsOfFile: or similar, but it doesn't look like you're guaranteed to get it from the code you've shown. (*)听起来好像您具有完整的数据,因为您说可以将其写入文件,然后使用dictionaryWithContentsOfFile:或类似文件读取,但看起来并不能保证从代码中获取数据你已经展示了。 You're creating a new data in -connection:didReceiveData: , but that delegate method can be called multiple times as the data arrives in pieces. 您将在-connection:didReceiveData:创建一个新数据,但是当数据分段到达时,可以多次调用该委托方法。 (I'm guessing it just happened to arrive all in one piece for your testing... this may not always be true, especially on a mobile device.) Instead, you probably want to create an empty mutable data when you start your NSURLConnection (or in -connection:didReceiveResponse: ), append to it in -connection:didReceiveData: , and parse it in -connectiondidFinishLoading: . (我猜想它恰好一次全部到达您的测试中……这可能并不总是正确的,尤其是在移动设备上。)相反,您可能想在启动NSURLConnection时创建一个空的可变数据(或在-connection:didReceiveResponse:追加到它在-connection:didReceiveData:和在解析它-connectiondidFinishLoading: Or even better, since the property list parser can't do anything with a partial data anyway, use the new +[NSURLConnection sendAsynchronousRequest:queue:completionHandler:] if you're targeting iOS 5.0+. 甚至更好的是,由于属性列表解析器仍然无法对部分数据执行任何操作,因此如果您的目标是iOS 5.0+,请使用新的+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM