下载远程PLIST iOS - 返回Null

时间:2013-05-25 12:21:45

标签: iphone ios mobile plist

挣扎着这个。希望下载一个只读的plist,以便在我的应用程序中使用...

无论如何努力阅读下面的词典...下面的代码,任何指针都会很棒..

-(void)loadProducts {


NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"https://dl.dropboxusercontent.com/s/f1nmq1wa7gs96dp/Region1Products.plist"]];


for (NSDictionary* productDictionary in dict) {
    ProductItem* productItem = [[ProductItem alloc] init];

    productItem.picturesCount = [productDictionary objectForKey:@"PicturesCount"];
    productItem.maxPicturesCount = [productDictionary objectForKey:@"MaxPicturesCount"];
    productItem.size = [productDictionary objectForKey:@"Size"];
    productItem.previewImageName = [productDictionary objectForKey:@"ImageName"];
    productItem.sequence = [productDictionary objectForKey:@"Sequence"];
    productItem.productName = [productDictionary objectForKey:@"Name"];
    productItem.type = [productDictionary objectForKey:@"ProductType"];
    productItem.prices = [productDictionary objectForKey:@"Prices"];
    productItem.shippingPrices = [productDictionary objectForKey:@"ShippingPrices"];
    productItem.description = [productDictionary objectForKey:@"Description"];
    productItem.popupMessage = [productDictionary objectForKey:@"PopupMessage"];
    productItem.popupDetailMessage = [productDictionary objectForKey:@"PopupDetailMessage"];
    productItem.incrementalPricing = [[productDictionary objectForKey:@"IncrementalPricing"] boolValue];
    if (YES == productItem.incrementalPricing) {
        productItem.incrementalPrices = [productDictionary objectForKey:@"IncrementalPrices"];
    }

    NSArray *previewItems = [productDictionary objectForKey:@"PreviewItems"];
    for (NSDictionary* previewItem in previewItems) {
        [productItem addProductPreviewItemFromDictionary:previewItem];
    }

    [self.productsList addObject:productItem];
}

1 个答案:

答案 0 :(得分:1)

您正在从网址创建NSDictionary,但该plist中的顶级对象是一个数组。要解决此问题,您可以将dict更改为NSArray对象。我使用以下简化代码来测试它:

NSArray *array = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"https://dl.dropboxusercontent.com/s/f1nmq1wa7gs96dp/Region1Products.plist"]];

for (NSDictionary* productDictionary in array) {
    NSLog(@"%@",productDictionary);
}