我在 View 中的回答一直是 0/0。 我不确定为什么它不更新? 我敢肯定有人会快速查看并立即解决它。我是一个完全的初学者,所以我被难住了。
#import "Fraction.h"
@implementation Fraction
{
#pragma mark --Step#3---What are the variables
int numerator;
int denominator;
}
-(void) setNumerator: (int) setNumerator
{
#pragma mark --Step#8---Make sure the setter is on the right
/* I had these back to front before */
NSLog (@"setNumerator %d ", setNumerator);
setNumerator = numerator;
}
-(void) setDenominator: (int) setDenominator
{
NSLog (@"setDenominator %d ", setDenominator);
setDenominator = denominator;
}
#pragma mark --Step#2---Copy over the methods and set them up
-(NSString*)print
{
NSString* calStr = [NSString stringWithFormat:@" %i/%i ",numerator,denominator];
return calStr;
}
@end
在接口(interface) .h 中实现 @property(assign)/* 之后 */ 来自@Anoop Vaidya 谢谢! :)
标题看起来像这样:
#import <Foundation/Foundation.h>
@interface Fraction : NSObject
#pragma mark --Step#1---What are the methods
/* what is this program going to do?*/
@property (assign) NSInteger numerator; //NSInteger is typedef to int
@property (assign) NSInteger denominator;
-(NSString*)print;
@end
和 .h 文件:
#import "Fraction.h"
@implementation Fraction
/* don't need these with the @property (assign) */
//-(void) setNumerator: (int) setNumerator
//{
// #pragma mark --Step#8---Make sure the setter is on the right
// /* I had these back to front before */
// NSLog (@"setNumerator %d ", setNumerator);
// _numerator = setNumerator;
//
//}
//-(void) setDenominator: (int) setDenominator
//{
// NSLog (@"setDenominator %d ", setDenominator);
// _denominator = setDenominator;
//
//}
#pragma mark --Step#2---Copy over the methods and set them up
-(NSString*)print
{
NSString* calStr = [NSString stringWithFormat:@" %i/%i ",_numerator,_denominator];
return calStr;
}
@end
最佳答案
代替:
setNumerator = numerator;
和
setDenominator = denominator;
换一种方式:
numerator = setNumerator;
和
denominator = setDenominator;
关于ios - 尝试用 ViewController 做一个简单的分数虽然难倒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20493340/