#定义或常量字符串*
我知道这个问题已经被问过好几次了,但我的略有不同。在将其作为重复项关闭之前,请完整阅读。 堆栈溢出上有很多帖子说:“就我个人而言,我讨厌 MACROS,不要使用那个狗屎”。我读过所有这些,我的情况有所不同。我正在尝试使用 #define 宏定义软件(iOS 应用程序)中使用的 URL。
我同意使用 const 字符串是比 #define 宏更好的做法。但在越来越多地基于 REST 的 API 世界中,接受查询参数作为 URL 的一部分,您如何仍然使用 const 字符串来表示发生变化的 URL?
而不是 http://api.myblog.com/posts?entryid=%@ 遵循 REST 原则的 API 服务器 http://api.blog.com/posts/entries/[entryid]
在前一种类型中,所有条目的 URL 为 http://api.myblog.com/posts,并且它们不要改变。常量字符串是可能的。
在后一种类型中,URL 随着每个条目而变化,我使用一个宏来扩展为这样的完整 URL。
#define GET_ENTRY_URL(__MY_ENTRY_ID__) [NSString stringWithFormat:@"http://api.myblog.com/posts/entries/%@", __MY_ENTRY_ID__];
我的方法有设计缺陷吗? 想知道您的意见。
谢谢。
I know this question has been asked several times, but mine is slightly different. Before closing this as duplicate, please read it fully.
There are many posts on stack overflow that says, "Personally, I hate MACROS, Don't use that shit". I've read all those and my case is different. I'm trying to define URLs used in a software (iOS app) using #define macros.
I agree that using const strings is a better practice than #define macros. But in an increasingly REST based API world that accepts query parameters as a part of URL, how can you still use const strings to represent a URL that changes?
Instead of http://api.myblog.com/posts?entryid=%@
a API Server that following REST principles would have
http://api.blog.com/posts/entries/[entryid]
In the former type, URL is http://api.myblog.com/posts for all entries and they don't change. A const string was possible.
In the latter type, URL changes with every entry and I use a Macro that expands to a full URL like this.
#define GET_ENTRY_URL(__MY_ENTRY_ID__) [NSString stringWithFormat:@"http://api.myblog.com/posts/entries/%@", __MY_ENTRY_ID__];
Are there any design flaws in my method?
Would like to know your inputs.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从编译器的角度来看,#define是一个预处理器指令(参考c中的定义,http:// /en.wikipedia.org/wiki/C_preprocessor)。
在这种情况下,编译器可能会在编译代码之前进行整个文本替换。
例如:如果您定义:
它可能会在代码中将每次出现的 GET_ENTRY_URL(x) 替换为 [NSString ..., x] 。如果 Objective-c 的实现遵循这一点,则可能会在我们使用宏的任何地方创建实例。
static const/variable 似乎是更好的方法。
Looking from the perspective of the compiler, #define is a preprocessor directive (refer to the definition in c, http://en.wikipedia.org/wiki/C_preprocessor).
In this case, compiler might be doing the whole text replacement before compiling your codes.
e.g.: if you define:
it could be replacing every occurrences of GET_ENTRY_URL(x) with [NSString ..., x] in your codes. Potentially, instances might be created everywhere we use the macro if the implementation of objective-c is following this.
static const/variable seems to be a better way.
我在应用程序中所做的是为基本路径定义一个 const ,为每个特定路径定义一个 const ,并在必要时在路径内替换格式代码。
然后,我在运行时为每个 API 构造 URL,如下所示:
What I did in my app was define a const for the base path and a const for each specific path with substitution format codes inside the path when necessary.
I then construct the URL at runtime for each API as follows: