在 Objective C 中将 NSMutableString 转换为 NSUrl

发布于 2024-10-18 00:31:53 字数 1458 浏览 1 评论 0原文

实际上,我在从 Objective-C 中的动态 url 解析 xml 文件时遇到了一个问题。 解析器对于整个项目来说工作得很好,但现在我必须设置一个动态 URL 来解析所需的 XML 文件。

我有两个变量:一个用于 BaseURL,一个用于 Params。 这是我记录的代码:

    //The Baseurl
NSMutableString* baselink = [NSMutableString stringWithString:zimmertyp.typlink];   

//Adds the params to URL
[baselink appendString:aSlice.link];

//In Log it shows the right url to my XML-File with params
NSLog(@"Selected-URL: %@", url);

//I tried to convert the String to NSURL here
NSURL *url = [NSURL URLWithString: baselink];


NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
self.navigationItem.prompt = @" ";

//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];

//Set delegate
[xmlParser setDelegate:parser];

//Start parsing the XML file.
BOOL success = [xmlParser parse];

if(success)
    NSLog(@"No Errors");
else
    NSLog(@"Error Error Error!!!"); 

每次解析器尝试遍历时,我都会得到:

2011-02-16 16:40:03.371 Project[15566:207] Selected-URL: http://xml.projectwebsite.de/price/doppelzimmer.xml
    ?Zimmertyp=Doppelzimmer+Classic
2011-02-16 16:40:03.373 Project[15566:207] Error Error Error!!!
2011-02-16 16:40:03.374 Project[15566:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString stringByAppendingString:]: nil argument'

NSUrl 似乎是空的,还是我错了? 当解析器使用 url 初始化时,应用程序崩溃了。

谁能告诉我我有什么错吗?

问候语, 扎拉卡斯

I actually stuck with a problem while parsing xml-file from a dynamic url in Objective-C.
The Parser works fine for the whole project, but now I have to set a dynamic URL to parse the needed XML-File.

I have two Variables: One for the BaseURL and one for the Params.
Here is my documented Code:

    //The Baseurl
NSMutableString* baselink = [NSMutableString stringWithString:zimmertyp.typlink];   

//Adds the params to URL
[baselink appendString:aSlice.link];

//In Log it shows the right url to my XML-File with params
NSLog(@"Selected-URL: %@", url);

//I tried to convert the String to NSURL here
NSURL *url = [NSURL URLWithString: baselink];


NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
self.navigationItem.prompt = @" ";

//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];

//Set delegate
[xmlParser setDelegate:parser];

//Start parsing the XML file.
BOOL success = [xmlParser parse];

if(success)
    NSLog(@"No Errors");
else
    NSLog(@"Error Error Error!!!"); 

Everytime the Parser tries to walk through, I get:

2011-02-16 16:40:03.371 Project[15566:207] Selected-URL: http://xml.projectwebsite.de/price/doppelzimmer.xml
    ?Zimmertyp=Doppelzimmer+Classic
2011-02-16 16:40:03.373 Project[15566:207] Error Error Error!!!
2011-02-16 16:40:03.374 Project[15566:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString stringByAppendingString:]: nil argument'

It seems like the NSUrl is empty, or am I wrong?
The App crashes right at the point when the Parser initialized with the url.

Can anyone tell me what's my fault?

greeting,
Zarakas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乙白 2024-10-25 00:31:53

我自己解决了这个问题。 AppendString 方法向字符串添加了空格...不知道为什么。我从字符串中转义了空格。现在解析器工作正常..;)

I solved the problem by myself. The AppendString method added whitespaces to the String... dunno why. I escaped the spaces from the string. Now parser works fine.. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文