문제

나는 이것을 여러 번했지만 이제는 실패합니다. 내 앱에서 배송 된 파일에 액세스하고 싶습니다.

NSString* path = [[NSBundle mainBundle] pathForResource:@"information" ofType:@"xml"];
.

를 반환합니다
/Users/eldude/Library/Application Support/iPhone Simulator/7.0.3/Applications/5969FF96-7023-4859-90C0-D4D03D25998D/App.app/information.xml
.

가 정확합니다 - 터미널 및 그 모든 것.그러나 경로를 구문 분석하려고 시도하면

    NSURL* fileURL = [NSURL URLWithString:path];

    NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:fileURL];

    [nsXmlParser setDelegate:self];

    if(![nsXmlParser parse]){
        NSError* e = nsXmlParser.parserError;
        NSLog(@"ERROR parsing XML file: \n %@",e);
        self = NULL;
    }
.

Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn’t be completed. (Cocoa error -1.)" UserInfo=0xb9256f0 {NSXMLParserErrorMessage=Could not open data stream}
.

아이디어 누군가?

도움이 되었습니까?

해결책

파일 URL 및 네트워크 URL이 다릅니다.
Apple 문서에서 :

중요 : 파일 시스템 경로에 대한 NSURL 객체를 만들려면
fileURLWithPath:isDirectory:
또는 그냥
fileURLWithPath:

예 :

NSString *filePath = @"path/file.txt";
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSLog(@"fileURL: %@", [fileURL absoluteURL]);

NSURL *netURL = [NSURL URLWithString:filePath];
NSLog(@"netURL: %@", [netURL absoluteURL]);
.

nslog 출력 :
fileurl : 파일 : ///path/file.txt

neturl : path / file.txt

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top