2013-12-10 51 views
0

我一直在我的答案獲得0/0在視圖中。 我不確定它爲什麼不更新? 我確定有人在那裏會有一個快速的樣子,並在一瞬間解決它。我是一個初學者,所以我很難過。試圖做一個簡單的分數與ViewController雖然難住

#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 

實現@property(分配)後/ *接口的.h */ 從@Anoop維迪謝謝! :)

頭看起來是這樣的:

#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 
+0

我該如何共享像@MatthiasBauch這樣的文件? –

+2

@GerardGrundy您應該將相關代碼直接複製/粘貼到問題中。 –

回答

1

相反的:

setNumerator = numerator; 

setDenominator = denominator; 

做另一種方式:

numerator = setNumerator; 

denominator = setDenominator; 
+0

那簡單!謝謝Greg。我非常感謝! –

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,所以不需要像你那樣明確地創建。

+0

感謝您的幫助:) –

+0

對不起,我正在嘗試:property(assign)numerator; property(assign)分母;雖然我不明白在哪裏放置它? –

+0

我正在嘗試----界面分數() property(assign)numerator; property(assign)分母; end --- sorry我無法添加@符號,因爲它會在堆棧溢出時產生錯誤 –

相關問題