首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSData initWithContentsOfFile为空

NSData initWithContentsOfFile为空
EN

Stack Overflow用户
提问于 2012-05-15 05:27:37
回答 1查看 989关注 0票数 1

我创建了一个简单的测试项目来解析XML。我在我的应用程序中为这个测试创建了一个.plist文件。我把它复制到documentsDirectory上。我可以看到文件被复制过来了。当我尝试将它初始化为NSXMLParser的NSData对象时,我得到:

代码语言:javascript
复制
The operation couldn’t be completed. (NSXMLParserErrorDomain error 4.)

这告诉我这个文件不存在。我检查了NSData是否为空,以及fileExistsAtPath是否为空,并且两者都检查得很好。我有点纠结于下一步该怎么做。有什么想法吗?谢谢。

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self createEditableCopyOfDatabaseIfNeeded];

    NSString *path = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"testList.plist"];

    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
if (data == nil) {
    NSLog(@"yes nil");
}
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)createEditableCopyOfDatabaseIfNeeded  
{ 
    // First, test for existence. 
    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 

    NSString *writableDBPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"testList.plist"]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) return; 
    // The writable database does not exist, so copy the default to the appropriate location. 
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"testList.plist"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) { 
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    }
}

- (NSString *)applicationDocumentsDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    return documentsDirectory;
}

#pragma mark - NSXMLParserDelegate
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    if ([elementName isEqualToString:@"hg19Packet0.bin"]) {
        NSLog(@"first");
    }
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"%@", [parseError localizedDescription]);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-15 06:30:51

你从文本编辑器看过你的样例plist了吗?如果你将它保存为一个二进制属性列表,它将不能使用NSXMLParser解析(我相信)。但是,您可以使用NSPropertyListSerialization来解析它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10591324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档