目标c上的解析错误

时间:2011-06-29 04:05:22

标签: objective-c

#import <Foundation/NSArray.h>
#import <Foundation/NSString.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSEnumerator.h>
#import <stdio.h>

void print( NSArray *array ) {
NSEnumerator *enumerator = [array objectEnumerator];
id obj;

while ( obj = [enumerator nextObject] ) {
    printf( "%s\n", [[obj description] cString] );
}
}

int main( int argc, const char *argv[] ) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *arr = [[NSArray alloc] initWithObjects:
                @"Me", @"Myself", @"I", nil];
NSMutableArray *mutable = [[NSMutableArray alloc] init];

// enumerate over items
printf( "----static array\n" );
print( arr );

// add stuff
[mutable addObject: @"One"];
[mutable addObject: @"Two"];
[mutable addObjectsFromArray: arr];
[mutable addObject: @"Three"];

// print em
printf( "----mutable array\n" );
print( mutable );

// sort then print
printf( "----sorted mutable array\n" );
[mutable sortUsingSelector: @selector( caseInsensitiveCompare: )];
print( mutable );

// free memory
[arr release];
[mutable release];
[pool release];

return 0;
}

这个程序在windows中用oscv0.1.4编译。 它给出了如下所示的错误

Error: Parse error on line 6:
...import <stdio.h>

void print( NSArray 
---------------------^
Expecting 'INTERFACE', 'IMPLEMENTATION', 'PROTOCOL', 'IMPORT', 'CLASS', 'DEFINE', 'EOF'

现在我为下面显示的程序又犯了一个错误(这是另一个程序)

#import "Forwarder.h"
#import "Recipient.h"

int main(void)
{
    Forwarder *forwarder = [Forwarder new];
    Recipient *recipient = [Recipient new];

[forwarder setRecipient:recipient]; //Set the recipient.
[forwarder hello];

[recipient release];
[forwarder release];

return 0;
}

错误是

Error: Parse error on line 3:
...Recipient : Object
- (id)hello;
@end#i
----------------------^
Expecting '<', '{'

1 个答案:

答案 0 :(得分:1)

程序的格式必须与下面显示的代码类似 这里main必须包含Main类 将此与链接进行比较 http://code.google.com/p/oscompiler/downloads/list

@interface Main : NSObject { }
@end

@implementation Main
+(void)main {
NSLog(@"Hello world!");
}
@end