【问题标题】:Storing constants and appending strings with iOS使用 iOS 存储常量和附加字符串
【发布时间】:2014-08-29 03:24:58
【问题描述】:

我创建了一个 constants.h 文件来在我的 iOS 应用程序中存储一些全局变量。但是,当我尝试在我的 ViewController 中访问它并附加到它时,我得到了很多错误。

这就是我的 constant.h 文件的样子:

#import <Foundation/Foundation.h>

@interface constants

#define projectURL @"http://website.com/projects";

@end

然后在我的视图控制器中,我在顶部导入常量文件:

#include "constants.h"

但是当我尝试使用以下内容记录网址时:

NSLog(@"%@", projectURL);

我得到错误:

Expected ')'

最终,我想做的是这样的:

NSString *newProjecURLString = [projectURL stringByAppendingFormat: @"/new?auth_token=%@", auth_token];

但是为此,我得到了错误:

Extraneous ')' before ';'

【问题讨论】:

  • 其他人怎么说。另外请注意,将#define 放在@interface 中是没有意义的——#define 是一个预处理器指令,并且不知道@funnyface 中的@interface

标签: ios objective-c string constants


【解决方案1】:

这是因为你的 #define 末尾有一个 ; 所以你要结束你的行然后添加 );

这是编译器看到的:

NSLog(@"%@", @"http://website.com/projects";);

【讨论】:

    【解决方案2】:

    #define projectURL @"http://website.com/projects"; 只是在编译前替换文本。

    在编译你的代码之前——我们称之为预编译

    clang 会将您代码中的所有projectURL 替换为@"http://website.com/projects";

    所以NSLog(@"%@", projectURL); 会变成NSLog(@"%@", @"http://website.com/projects";);

    【讨论】:

      猜你喜欢
      • 2011-10-27
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      相关资源
      最近更新 更多