下面的代码,编译的时候报错,也就是红色代码部分
MYHelloWorld.h:7:1:error:stray '@' in program
MYHelloWorld.m:5:1:error:stray '@' in program
- C/C++ code
#import <Foundation/Foundation.h> @interface MYHelloWorld : NSObject{ NSString* _userName; } [color=#FF0000]@property(nonatomic,retain)NSString* userName;[/color] - (id)initWithUserName:(NSString*)userName; - (void)helloWorld; @end
- C/C++ code
#import "MYHelloWorld.h" @implementation MYHelloWorld [color=#FF0000]@synthesize userName = _userName;[/color] - (id)initWithUserName:(NSString *)userName{ self = [super init]; _userName = [userName copy]; return self; } - (void)helloWorld{ NSString *text = [NSString stringWithFormat:@"%@,Hello World!",_userName]; printf([text UTF8String]); } - (void)dealloc{ [_userName release]; [super dealloc]; } @end
------解决方案--------------------
e ...printf函数要包含c的iostd库吧
------解决方案--------------------
printf換成NSLog
------解决方案--------------------
没有错误。
检查拼写和中英文标点是否有误。
------解决方案--------------------
命名尽量统一吧。。?
------解决方案--------------------
- C/C++ code
#import "MYHelloWorld.h" @implementation MYHelloWorld @synthesize userName ; - (id)initWithUserName:(NSString *)userName{ self = [super init]; _userName = [userName copy]; return self; } - (void)helloWorld{ NSString *text = [NSString stringWithFormat:@"%@,Hello World!",_userName]; printf([text UTF8String]); } - (void)dealloc{ [_userName release]; [super dealloc]; } @end
------解决方案--------------------
_userName如果不是alloc的就不要release了吧。。。
------解决方案--------------------
@property(copy)NSString* userName;
------解决方案--------------------
关注一下。。。。。。