繁体   English   中英

尝试用ViewController做一个简单的分数虽然难倒

[英]Trying to do a simple fraction with ViewController though stumped

我继续在视图中获得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

在@Anoop Vaidya的接口.h * /中实现@property(assign)/ *之后谢谢! :)

标题看起来像这样:

#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;

我想给你带来一些信息(你似乎在学习):

-(void) setNumerator: (int) setNumerator;

对于Apple命名约定来说,这是一个非常糟糕的方法名称。 它应该是:

-(void)numerator: (int)aNumerator;

你也在这里做了一个反向任务:

-(void) numerator: (int)aNumerator{
    numerator = aNumerator;       
}

这两个不是在实现中创建:

int numerator;
int denominator;

您应该在界面中创建它:

@interface Fraction : NSObject

@property (assign) NSInteger numerator; //NSInteger is typedef to int
@property (assign) NSInteger denominator;

@end

因为这会为你设置setter和getter,所以不需要像你那样明确地创建。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM