我是 Objective-C 的新手(以及一般的编程……我只学过 Python 和一点点 HTML)。我正在尝试从教程 here 中学习 但我遇到了一个我似乎无法弄清楚的错误。
在 Fraction.m 中,在 - (void)add:(Fraction *)newFraction 下,接下来的两行出现错误“No getter method for read from property”。
如果有人能帮助我,那就太好了!任何建议表示赞赏。
谢谢, 亚当
代码:
分数演示.m
#import "Fraction.h"
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
Fraction *aFraction = [[Fraction alloc] init];
Fraction *bFraction = [[Fraction alloc] init];
[aFraction setNumerator:1 overDenominator:2];
[bFraction setNumerator:1 overDenominator:3];
[aFraction display]; NSLog(@" + "); [bFraction display]; NSLog(@" = ");
[aFraction add:bFraction];
[aFraction display];
}
return 0;
}
分数.h
#import <Foundation/Foundation.h>
@interface Fraction : NSObject {
NSInteger numerator;
NSInteger denominator;
}
- (void)setNumerator:(NSInteger)value;
- (void)setDenominator:(NSInteger)value;
- (void)display;
- (void)setNumerator:(NSInteger)num overDenominator:(NSInteger)denom;
- (void)add:(Fraction *)newFraction;
@end
分数.m
#import "Fraction.h"
@implementation Fraction
- (void)setNumerator:(NSInteger)value {
numerator = value;
}
- (void)setDenominator:(NSInteger)value {
denominator = value;
}
- (void)display {
NSString *numeratorString = [[NSString alloc] initWithFormat:@"%ld", (long)numerator];
NSString *denominatorString = [[NSString alloc] initWithFormat:@"%ld", (long)denominator];
NSLog(@"%@/%@", numeratorString, denominatorString);
}
- (void)setNumerator:(NSInteger)num overDenominator:(NSInteger)denom {
self.numerator = num;
self.denominator = denom;
}
- (void)add:(Fraction *)newFraction {
// a/b + c/d = ((a * d) + (b * c)) / (b * d)
self.numerator = self.numerator * newFraction.denominator + self.denominator * newFraction.numerator;
self.denominator = self.denominator * newFraction.denominator;
}
@end
最佳答案
有两种可能的解决方案
解决方案一
将所有 self.numerator 更改为 numerator 并将所有 self.denominator 更改为 denominator
方案二
将numerator 和denominator 声明为属性
@property(nonatomic) NSInteger numerator, denominator;
现在将所有 numerator 更改为 self.numerator 并将所有 denominator 更改为 self.denominator
我建议的是解决方案 2
解释
您收到错误的原因是,在您的原始代码中,分子和分母是类变量。因此,您可以在不使用 self. 的情况下使用相同的名称访问它们。错误是在您使用 self.numerator 的行上引发的。
解决方案 1 的一个缺点是您需要为这些变量添加自己的 setter 和 getter 方法。但是在解决方案 2 中,通过将它们声明为属性,操作系统将只创建 setter 和 getter 方法。您唯一需要做的就是使用 self. 访问变量。
在将它们声明为属性后,普通 numerator 将不起作用。您需要使用 self.numerator。这就是您再次出错的原因。正如我已经说过的,如果您将它们声明为属性,则不需要添加 getter 和 setter 方法,因为操作系统本身正在创建它们。所以你可以简单地删除 setNumerator 和 setDenominator 方法
关于ios - Objective-C 没有 getter 方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38209721/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案