简体   繁体   English

iOS-如何将细分添加到NSURL?

[英]IOS - How do I add segments to NSURL?

Suppose you have a URL that looks like http://localhost:5867 and your wanted to append '/something' to the url. 假设您有一个看起来像http://localhost:5867的URL,并且想要在该URL http://localhost:5867附加“ / something”。 How do I append segments to this URL? 如何将细分附加到此URL? I am trying this: 我正在尝试:

//_baseURL is NSURL* and documentid is NSString*
NSURL* url = [_baseURL URLByAppendingPathComponent:documentid];

Oddly.. xcode(4.6.2) will not output the resulting URL to console. 奇怪的是.. xcode(4.6.2)不会将结果URL输出到控制台。

 NSLog(@"%@", [url absoluteString]);//nothing output

Thanks! 谢谢!

Edit 编辑

I am working in a workspace. 我在工作区中工作。 The code that I am debugging is a static lib which is in the workspace. 我正在调试的代码是工作空间中的静态库。 Could this be the culprit of all this weirdness?? 这可能是所有这些怪异的罪魁祸首吗?

Fixed 固定

I think the reason I was having problems was because my workspace was not configured correctly and I was actually executing an older version of my static lib. 我认为我遇到问题的原因是因为我的工作区配置不正确,而我实际上正在执行旧版本的静态库。 This explains why I wasn't getting NSLog'ing and why the result were unexpected. 这解释了为什么我没有得到NSLog'ing以及为什么结果出乎意料。 I found this post which helped me understand the workspace and how to configure it for a static library. 我发现这篇文章有助于我了解工作区以及如何为静态库配置它。 Thanks a lot for everyone's time! 非常感谢大家的时间!

There is a class message, 有一个班级消息,

+ (id)URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL

So, 所以,

NSURL *base  = [NSURL URLWithString: @"http://localhost:5867"];
NSURL *child = [NSURL URLWithString: @"something" relativeToURL:base];
NSURL *base  = [NSURL URLWithString:@"http://localhost:5867"];
NSURL *url = [base URLByAppendingPathComponent:@"something"];

NSLog(@"%@", [url absoluteString]);

This works for me and prints http://localhost:5867/something , so check your variables (maybe one of them is nil or an empty string?). 这对我http://localhost:5867/something ,并显示http://localhost:5867/something ,所以检查您的变量(也许其中之一是nil还是空字符串?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM