★★一段objective-c的代码,错在哪里?
下面的代码,编译的时候报错,也就是红色代码部分
MYHelloWorld.h:7:1:error:stray '@' in program
MYHelloWorld.m:5:1:error:stray '@' in program
#import <Foundation/Foundation.h>@interface MYHelloWorld : NSObject{ NSString* _userName;}[color=#FF0000]@property(nonatomic,retain)NSString* userName;[/color]- (id)initWithUserName:(NSString*)userName;- (void)helloWorld;@end
#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
#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;
[解决办法]
关注一下。。。。。。