【问题标题】:error: stray ‘@’ in program and others with NSArray错误:程序中的“@”和其他带有 NSArray 的杂散
【发布时间】:2018-07-21 17:55:31
【问题描述】:

我一直在努力学习 Cool school Objective-C 课程,但在 10/11 的挑战中遇到了困难。它要我创建一个数组变量,然后访问这些值。这是它给我的:

NSArray *apps = @[@"AngryFowl", @"Lettertouch", @"Tweetrobot"];
NSLog (@"%@", apps[1]);

在线编译器抛出这个:

main.m: In function ‘main’:
main.m:7:17: error: stray ‘@’ in program
 NSArray *apps = @[@"AngryFowl", @"Lettertouch", @"Tweetrobot"];
                 ^
main.m:7:31: warning: left-hand operand of comma expression has no effect [-Wunused-value]
 NSArray *apps = @[@"AngryFowl", @"Lettertouch", @"Tweetrobot"];
                               ^
main.m:7:47: warning: left-hand operand of comma expression has no effect [-Wunused-value]
 NSArray *apps = @[@"AngryFowl", @"Lettertouch", @"Tweetrobot"];
                                               ^
main.m:7:62: error: expected ‘:’ before ‘]’ token
 NSArray *apps = @[@"AngryFowl", @"Lettertouch", @"Tweetrobot"];
                                                              ^  

我的问题可能是什么?

【问题讨论】:

  • Objective-C literals 自 Apple LLVM Compiler 4.0 (AFAIR Xcode 6) 起可用。在线编译器显然不支持它们。
  • 谢谢!!拿出笔记本电脑并在 xcode 中完成它,它工作了
  • 对于在线编译器你必须使用经典的[NSArray arrayWithObjects:@"AngryFowl", @"Lettertouch", @"Tweetrobot", nil];

标签: objective-c nsarray


【解决方案1】:

当我使用http://rextester.com/l/objectivec_online_compiler 时,我遇到了同样的错误:

//gcc 5.0.4

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSArray *apps = [NSArray arrayWithObjects:@"AngryFowl", @"Lettertouch", @"Tweetrobot", nil];
    NSLog (@"%@", [apps objectAtIndex:1]);
    [pool drain];
    return 0;
}

当我使用时

arrayWithObjects

objectAtIndex

有效:

2018-10-05 06:11:35.286 a.out[21957] Lettertouch

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    相关资源
    最近更新 更多