【问题标题】:How do I go about placing an object in a #define statement如何在#define 语句中放置对象
【发布时间】:2012-08-10 19:38:43
【问题描述】:

如何在#define 语句中放置对象,如下所示:

#define PlacesURL @"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=**bar**&sensor=false&key=MyAPIKey"

所以,代替 bar 我想做类似 %@ 的操作,但不确定如何存储 %@ 对象。另外,我将在哪里存储对象 - 在#define 指令上方?我想存储酒吧、餐馆、咖啡店等...以便通过 iOS 应用进行 Google Places Api 搜索。

【问题讨论】:

  • 你不能使用:[NSString stringWithFormat: PlacesURL, theTypeString];如果您将 bar 替换为 %@

标签: ios objective-c iphone c-preprocessor


【解决方案1】:

您应该遵循以下代码:

#define PlacesURL(value) ([NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=%@&sensor=false&key=MyAPIKey", value])

#define 语句必须有方括号。让我们看看下面的例子。您必须返回预期的结果是 900,但 230 会 出现。这只是一种示例。你一定不要忘记 括号。括号中句子的开头和结尾,如 以及每个句子的结尾必须在适当的位置。

#define A 10
#define B 20
#define C A+B
// not 900, result value is 230. 'Cause, proceed as follows: A+B*A+B
NSLog(@"value:%d", C*C); 

【讨论】:

    【解决方案2】:

    据我了解,您有两种选择:

    #define PLACES     @"bar,restaurants"
    #define PlacesURL  ([NSString stringWithFormat: @"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=%@&sensor=false&key=MyAPIKey", PLACES])
    

    #define PlacesURL( places ) ([NSString stringWithFormat: @"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=%@&sensor=false&key=MyAPIKey", places])
    

    第二个可能是你想要的,虽然两者都有点没有意义......

    【讨论】:

      【解决方案3】:

      在 C 中,任何两个列在一起的常量字符串都被认为是一个字符串,因此以下任何一个都是等价的:

      a = "abc123xyz";
      
      b = "abc" "123" "xyz";
      
      #define FOO "123"
      c = "abc" FOO "xyz";
      

      所以,你可以像这样做你想做的事:

      #define BAR "whatever"
      #define PlacesURL "https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=" BAR "&sensor=false&key=MyAPIKey"
      

      但是,这似乎是一件很奇怪的事情?您是否尝试在运行时粘贴栏名称?如果是这样,您需要使用 sprintf 来完成(注意 PlacesURL 中的 %s):

      #define PlacesURL "https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=%s&sensor=false&key=MyAPIKey"
      
      char *get_url(char *bar) {
        char url[1000];
      
        sprintf (url, PlacesURL, bar);
        return strdup(url);
      }
      

      调用函数完成后必须free(url)

      【讨论】:

      • 您的 #define Bar 然后 #define PlacesURL 将不起作用(正如 OP 所期望的那样),因为它返回一个 C 字符串而不是 NSString 对象。第二个选项是 Dito。
      • @Paul de Lange:相邻的 NSStrings 会像 C 字符串一样自动连接,所以可以转换第一个选项。
      • +1 @W'rkncacnter 在这个缓慢的下午教我一些东西:)
      • 是的,我不知道iOS使用什么语言,所以我写了“In C ...”。在我发布后我意识到有一个“objective-c”标签。我知道 Objective-C 是 C 的超集,所以我认为它会起作用,但是,正如你所说,只能用于转换。
      • @ams:嗯,在这种情况下唯一需要的转换是将@ 添加到字符串的前面(实际上,它只需要在 first组的字符串):@"Dost" "oyevsky"
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-27
      • 2012-01-29
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多