实现上下文中缺少“@end”

时间:2012-04-23 20:31:05

标签: objective-c

有两个与实现有关的错误。已经注释掉了错误。

Header file: 
#import <Foundation/Foundation.h>

@interface Appliance : NSObject {
  NSString *productName;
  int voltage;
}

@property (copy)NSString *productName;
@property int voltage;
-(id)initWithProductName:(NSString *)pn;

@end

实施档案:

#import "Appliance.h"

@implementation Appliance //'@end' is missing in implementation context

@synthesize productName, voltage;

-(id)initWithProductName:(NSString *)pn
{
  // Call the NSObject's init method
  self = [super init];

  // Did it return something non-nil?
  if (self) {

    // Set the product name
    [self setProductName:pn];

    // Give voltage a starting value
    [self setVoltage:120];

  // Return a pointer to the new object
  return self;
}
@end // unexpected '@' in program

1 个答案:

答案 0 :(得分:6)

您忘了关闭if块

if (self) {

    // Set the product name
    [self setProductName:pn];

    // Give voltage a starting value
    [self setVoltage:120];
} // << missing