当前位置: 代码迷 >> Iphone >> ★一段objective-c的代码,错在哪里?解决方法
  详细解决方案

★一段objective-c的代码,错在哪里?解决方法

热度:156   发布时间:2016-04-25 06:48:43.0
★★一段objective-c的代码,错在哪里?
下面的代码,编译的时候报错,也就是红色代码部分
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;
------解决方案--------------------
关注一下。。。。。。
  相关解决方案