【问题标题】:Beginner: Objective C errors in XCode初学者:XCode 中的 Objective C 错误
【发布时间】:2014-11-09 19:35:59
【问题描述】:

编辑:感谢 Onik IV 和 Rob Bajorek!

我正在通过一本名为 Objective C for Absolute Beginners 的书来学习 Obj C。我应该为广播电台创建一个代码并且遇到了一些错误。这有点令人困惑,因为我正在编写与书中显示的代码相同的代码。

头文件:https://github.com/williyam/XCode-Projects/blob/master/radiostations/RadioStations/RadioStations.h

方法文件:https://github.com/williyam/XCode-Projects/blob/master/radiostations/RadioStations/RadioStations.m

XCode 在方法文件中抛出这些错误:

  • 未找到“setFrequency”、“name”、“setName”和“frequency”的第 11 行方法定义
  • 第 33 行无效的参数类型 'NSString *' 到一元表达式

标题

#import <Foundation/Foundation.h>

@interface RadioStations : NSObject{
    NSString* name;
    double frequency;
    NSUInteger band;
}

+ (double)minAMFrequency;
+ (double)maxAMFrequency;
+ (double)minFMFrequency;
+ (double)maxFMFrequency;

- (id)initWithName: (NSString*)newName atFrequency:(double)newFrequency;
- (NSString*)name;
- (void)setName: (NSString*)newName;
- (double)frequency;
- (void)setFrequency:(double)newFrequency;


@end

方法

#import "RadioStations.h"

@implementation RadioStations
+ (double)minAMFrequency{
    return 520.0;
}
+ (double)maxAMFrequency{
    return 1610.0;
}
+ (double)minFMFrequency{
    return 88.3;
}
+ (double)maxFMFrequency{
    return 107.9;
}

- (id)initWithName: (NSString*)newName atFrequency:(double)newFrequency {
    self = [super init];
    if(self!=nil){
        name = newName;
        frequency = newFrequency;
    }
    return self;

- (NSString*)newName{
    return name;
}
- (void)setName:(NSString*)newName{
    name = newName;
}
- (double)frequency{
    return frequency;
}
- (void)setFrequency: (double)newFrequency{
    frequency = newFrequency;
}
}

    @end

【问题讨论】:

  • 不要发布指向您的代码的链接,而是在您的问题中发布相关代码(不是全部)。
  • 抱歉,这是我第一次来这里和代码打交道。这是标题和方法。我会编辑它
  • 应该在return self 之后的右大括号字符改为位于文件底部。

标签: objective-c xcode


【解决方案1】:

我看到了几个错误:

  #import "RadioStations.h"

 .....
// Change newName to name. (like in header says)


// Change this  - (NSString*)newName{
- (NSString*)name{

return name;
 }


......

- (void)setFrequency: (double)newFrequency{
frequency = newFrequency;
}
// This must be delete }

@end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    相关资源
    最近更新 更多