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

标签 ios objective-c uiviewcontroller

我在 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/

相关文章:

ios - 如何从重复的 JSON 键 iOS 中获取值

iphone - 初始化纵向和横向的 UIViewController

ios - 如何在 UIScrollview 中嵌入 UITableView

ios - 覆盖 UIViewConrtoller 扩展中的 PreferredStatusBarStyle

iphone - 在 Xcode Storyboard中使用可拉伸(stretch)图像

ios - 在 iOS 上增加计步器初始响应

ios - native 代码 + 网站中的 Phonegap 构建

ios - 设置约束后获得正确的框架

ios - 如何在 iOS 6.1 中将字符串转换为 base64 编码

iOS - 内存警告卸载 View Controller 并使应用程序无响应