0

这似乎是一个完全有效的程序:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    NSString* hey = @"hey"
    @"there";
    NSLog(@"%@",hey);

    [pool drain];
    return 0;
}

在 llvm gcc 和 apple llvm 中编译它不会显示任何警告或错误。为什么?似乎应该警告这一点,因为我只能认为这是引起混乱的。尤其是在这样的情况下:

NSArray* a = [NSArray arrayWithObjects:@"one",
              @"two,
              @"three
              @"four",
              nil];

你会期待四个条目......但没有!棘手,是吗?是因为您可能希望将字符串定义拆分为多行吗?

4

2 回答 2

2

它是 Objective-C 中多行字符串的语法。

我无法明确回答为什么语言设计者会这样设计它,但我们可以假设他们希望 Objective-C 字符串的语法类似于 C 字符串的语法。

也就是说,在 C 中,您可以对多行​​字符串执行以下操作:

char *my_string = "Line 1 "
                  "Line 2";

在 Objective-C 中,您可以对多行​​字符串执行以下操作:

NSString *my_string = @"Line1 "
                      @"Line2"; // The @ on this line is optional.

(代码片段改编自https://stackoverflow.com/a/797351/25295

于 2012-10-05T22:09:52.970 回答
1

是因为您可能希望将字符串定义拆分为多行吗?

是的。当您想要拆分非常长的字符串以更好地阅读代码时,它很有用,即在NSLocalizedString关键描述中。

于 2012-10-05T22:07:25.367 回答