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

时间:2013-12-10 11:31:38

标签: ios objective-c uiviewcontroller

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

在接口.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

2 个答案:

答案 0 :(得分:1)

而不是:

setNumerator = numerator;

setDenominator = denominator;

以其他方式做到:

numerator = setNumerator;

denominator = setDenominator;

答案 1 :(得分:1)

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

-(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,所以不需要像你那样明确地创建。