我一直在我的答案獲得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
我該如何共享像@MatthiasBauch這樣的文件? –
@GerardGrundy您應該將相關代碼直接複製/粘貼到問題中。 –