0

我对此代码有错误

 NSString *theURL = @"http://www.w3schools.com/xml/cd_catalog.xml";
        NSData *theData=[NSData dataWithContentsOfFile:[NSURL URLWithString:theURL]];
        NSDictionary *theDic=[XMLReader dictionaryForXMLData:theData error:nil];

错误是

 2013-02-10 00:39:05.113 MyXml[4139:c07] -[NSURL getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x7139670
2013-02-10 00:39:05.115 MyXml[4139:c07] *** Terminating app due to uncaught exception 

    'NSInvalidArgumentException', reason: '-[NSURL getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x7139670'
    *** First throw call stack:
    (0x1c96012 0x10d3e7e 0x1d214bd 0x1c85bbc 0x1c8594e 0xad3ee4 0xad3e92 0xad3de2 0xaf2336 0x2bb8 0x15157 0x15747 0x1694b 0x27cb5 0x28beb 0x1a698 0x1bf1df9 0x1bf1ad0 0x1c0bbf5 0x1c0b962 0x1c3cbb6 0x1c3bf44 0x1c3be1b 0x1617a 0x17ffc 0x295d 0x2885)
    libc++abi.dylib: terminate called throwing an exception
4

1 回答 1

0

它给出了错误,因为您正在通过 的帮助使用文件初始化 NSData dataWithContentsOfFile,但实际上您正在向 NSData 提供 URL 以进行初始化。因此,您的代码间接调用文件方法 getFileSystemRepresentation:maxLength:NSURL这就是它给出错误未知选择器的原因。

用这个dataWithContentsOfURL代替dataWithContentsOfFile

NSString *theURL = @"http://www.w3schools.com/xml/cd_catalog.xml";
        NSData *theData=[NSData dataWithContentsOfURL:[NSURL URLWithString:theURL]];
于 2013-02-10T06:01:34.850 回答