我检查了几条建议的链接,但未找到答案。我想将两个浮点值传递给对象breuk的初始化。该对象有一个初始化方法initwithTeller :( float)mijnTeller和Nomer:(float)mijnNoemer。该方法还返回浮点商。
首先,我收到一个错误,就像使用不兼容类型float的表达式初始化__strong一样。它是在方法berekenQuotient初始化对象breuk时给出的。
我还想知道是否有必要在breuk.h中声明属性mijnTeller和mijnNoemer,因为它们是通过berekenQuotient传递的。 tx!
代码:
viewcontroller.h:
#import <UIKit/UIKit.h>
#import "breuk.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *tellerVeld1;
@property (strong, nonatomic) IBOutlet UITextField *noemerVeld1;
@property (strong, nonatomic) IBOutlet UILabel *quotientVeld;
- (IBAction)berekenQuotient:(id)sender;
@end
viewcontroller.M
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize tellerVeld1;
@synthesize noemerVeld1;
@synthesize quotientVeld;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)berekenQuotient:(id)sender {
float teller = [tellerVeld1.text floatValue];
float noemer = [noemerVeld1.text floatValue];
breuk *breuk1 = [[breuk alloc]initWithTeller:teller andNoemer:noemer];
//float quotient = [tellerVeld1.text floatValue]/[noemerVeld1.text floatValue];
//quotientVeld.text = [[NSString alloc]initWithFormat:@"%.2f", quotient];
}
@end
breuk.h
#import <Foundation/Foundation.h>
@interface breuk : NSObject
@property float mijnTeller;
@property float mijnNoemer;
@property float quotient;
- (float) initWithTeller: (float) mijnTeller andNoemer:(float) mijnNoemer;
@end
breuk.m
#import "breuk.h"
@implementation breuk
@synthesize mijnTeller;
@synthesize mijnNoemer;
@synthesize quotient;
- (float)initWithTeller:(float)mijnTeller andNoemer:(float)mijnNoemer{
return quotient = mijnTeller/mijnNoemer;
}
@end
答案 0 :(得分:1)
所有init
方法都应该返回自己的类的实例,你想要的只是一个进行计算的类方法。所有课程都应以大写字母开头:
Breuk.h
#import <Foundation/Foundation.h>
@interface Breuk : NSObject
@property float mijnTeller;
@property float mijnNoemer;
@property (readonly) float quotient;
- (instancetype) initWithTeller: (float) mijnTeller andNoemer:(float) mijnNoemer;
@end
Breuk.m
#import "Breuk.h"
@implementation breuk
@synthesize mijnTeller;
@synthesize mijnNoemer;
@synthesize quotient;
- (id)initWithTeller:(float)mijnTeller andNoemer:(float)mijnNoemer{
self = [super init];
if (self) {
self.mijnTeller = mijnTeller;
self.mijnNoemer = mijnNoemer;
}
return self;
}
- (float) quotient {
return mijnTeller/mijnNoemer;
}
@end
或者只是使用类方法
Breuk.h
#import <Foundation/Foundation.h>
@interface Breuk : NSObject
+ (float) quotientWithTeller:(float)teller andNoemer:(float)noemer;
@end
Breuk.m
#import "Breuk.h"
@implementation breuk
+ (float) quotientWithTeller:(float)teller andNoemer:(float)noemer;
return teller/noemer;
}
@end
使用类方法,您不需要Breuk
类的实例,只需在类上调用它:
float quotient = [Breuk quotientWithTeller:teller andNoemer:noemer];
答案 1 :(得分:1)
您的init
方法无法正常工作。试试这个:
@interface Breuk : NSObject {
float _mijnTeller;
float _mijnNoemer;
}
@property float mijnTeller;
@property float mijnNoemer;
- (id)initWithTeller:(float)mijnTeller
andNoemer:(float)mijnNoemer;
- (float)quotient;
@end
@implementation Breuk
@synthesize mijnTeller = _mijnTeller;
@synthesize mijnNoemer = _mijnNoemer;
- (id)initWithTeller:(float)mijnTeller
andNoemer:(float)mijnNoemer {
self = [super init];
if (self) {
_mijnTeller = mijnTeller;
_mijnNoemer = mijnNoemer;
}
return self;
}
- (float)quotient {
return mijnTeller / mijnNoemer;
}
@end
答案 2 :(得分:0)
您收到错误使用不兼容类型float 的表达式初始化__strong,因为initWithTeller:andNoemer:
返回一个浮点数,但您将其分配给类型为breuk1
的{{1}} {1}}。默认情况下,breuk
等局部变量是强引用,这是错误的__strong部分所在的位置。
您没有 在breuk.h中声明属性breuk1
和mijnTeller
。如果要存储这些值,然后稍后再次使用它们,则只需要这样做。
我也认为你不需要首先尝试创建一个mijnNoemer
对象,你可以使用类方法来执行该计算。