开发者

Plist file exists in docs dir, but cannot be loaded into an array

The plist has the file suffix .xml and is a normal array of dictionaries.

In the app delegate:

#define docDir [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]

-(NSString *)cacheFile:(NSString *)filename sitepath:(NSString *)url {
    NSMutableString *retfn=[NSMutableString string];
    NSString *mediaUrl=[NSString stringWithFormat:@"%@%@",url,filename];
    if(nil != mediaUrl){
        NSData* imageData;
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        @try {
            imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mediaUrl]];
        }
        @catch (NSException * e) {  //error
            NSLog(@"%@",@"CacheFile error!");
        }
        @finally {  //save to Documents
            [retfn appendFormat:@"%@%@%@",docDir,@"/",filename];
            [imageData writeToFile:retfn atomically:YES];
        }
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [imageData release];
    }
    return retfn;
}

In my tableView controller:

#define fname @"myplist.xml"
#define fdir @"http://mysite.com/iPhoneApp/"

- (void)viewDidLoad {
    appdel=(EksjoHusAppDelegate *) [[UIApplication sharedApplication] delegate];
    NSString *husDataF=[appdel cacheFile:fname sitepath:fdir];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:husDataF];

    if (([husDataF length]>0)&&(fileExists)) {
        NSMutableArray 开发者_开发知识库*lochusData=[[NSMutableArray alloc] initWithContentsOfFile:husDataF];
        appdel.husData=lochusData;
        [lochusData release];
    }   
}

initWithContentsOfFile sets lochusData to 0x0. This the part of code I'm asking about, really. The rest is more for completion. cacheFile returns the local filepath or an empty string. All the values are correct, but it just won't load / go into the array.

Feeling dumb. I just don't see the error. Is there something I'm missing?


Answer was, "Check your charset when saving the generated XML, dude" :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜