Ios 如何将宏NSString转换为NSURL

Ios 如何将宏NSString转换为NSURL,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,如何将字符串转换为URL格式?定义这些宏 #define string = @"http://rtchubs.com/Client/PhotoStreamer/SaveRating.php?RateID=%@&&Rate=%@&&Photoid=%@" 用法 #define STRING @"http://rtchubs.com/Client/PhotoStreamer/SaveRating.php?RateID=%@&&Rate=%@&

如何将字符串转换为URL格式?

定义这些宏

#define string = @"http://rtchubs.com/Client/PhotoStreamer/SaveRating.php?RateID=%@&&Rate=%@&&Photoid=%@"
用法

#define STRING @"http://rtchubs.com/Client/PhotoStreamer/SaveRating.php?RateID=%@&&Rate=%@&&Photoid=%@"
#define URL_FROM_PARAMETER(_rateId_, _rate_, _photoId_) [NSURL URLWithString:[NSString stringWithFormat:STRING, _rateId_, _rate_, _photoId_]]
输出:

id rateId = @"rateid", rate = @"rate",photoId = @"photoid";
NSURL *url = URL_FROM_PARAMETER(rateId, rate, photoId);
NSLog(@"%@", url);

我只想创建一个纯助手函数,而不必关心如何使用URL路径在代码库中构建正确的URL。

http://rtchubs.com/Client/PhotoStreamer/SaveRating.php?RateID=rateid&&Rate=rate&&Photoid=photoid

您是否需要转换为NSUrl???@EmelElias是,我想在字符串中插入%@文字值为什么或者为什么它必须是宏?@Sulthan,因为我想声明它globally@user3505490考虑使用const NSnpe代替——在这种情况下不需要宏,在许多开发人员中,它被认为是这些场景的坏样式。之所以认为它的风格不好,部分原因在于宏不是类型安全的。这是一个很好的答案,但这与我在互联网上写这类东西的更好判断背道而驰,因为有些人实际上可以这样开始使用宏……:)
// .h
extern NSURL *TLARateURL(NSString *rateID, NSString *rate, NSString *photoID);

// .c/m
NSURL *TLARateURL(NSString *rateID, NSString *rate, NSString *photoID) {
  static NSString *format = @"http://rtchubs.com/Client/PhotoStreamer/SaveRating.php?RateID=%@&&Rate=%@&&Photoid=%@";
  return [NSURL URLWithString:[NSString stringWithFormat:format, rateID, rate, photoID]];
}