Pfad zur Datei im App-Bundle
-
20-12-2019 - |
Frage
Ich habe das schon oft gemacht, aber jetzt schlägt es fehl.Ich möchte auf eine Datei zugreifen, die in meiner App enthalten ist.
NSString* path = [[NSBundle mainBundle] pathForResource:@"information" ofType:@"xml"];
kehrt zurück
/Users/eldude/Library/Application Support/iPhone Simulator/7.0.3/Applications/5969FF96-7023-4859-90C0-D4D03D25998D/App.app/information.xml
Das ist richtig - im Terminal eingecheckt und so weiter.Der Versuch, den Pfad zu analysieren, schlägt jedoch fehl
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;
}
mit
Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn’t be completed. (Cocoa error -1.)" UserInfo=0xb9256f0 {NSXMLParserErrorMessage=Could not open data stream}
Hat jemand eine Idee?
Lösung
Datei-URLs und Netzwerk-URLs sind unterschiedlich.
Aus der Apple-Dokumentation:
Wichtig:Um NSURL-Objekte für Dateisystempfade zu erstellen, verwenden Sie
fileURLWithPath:isDirectory:
oder nur
fileURLWithPath:
Beispiel:
NSString *filePath = @"path/file.txt";
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSLog(@"fileURL: %@", [fileURL absoluteURL]);
NSURL *netURL = [NSURL URLWithString:filePath];
NSLog(@"netURL: %@", [netURL absoluteURL]);
NSLog-Ausgabe:
DateiURL:file:///path/file.txt
netURL:Pfad/Datei.txt
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow