我检查了几个建议的链接,但没有找到答案。我想将两个浮点值传递给对象 breuk 的初始化。该对象有一个初始化方法 initwithTeller: (float) mijnTeller and Noemer: (float) mijnNoemer。该方法还返回一个浮点商。
首先,在使用不兼容类型浮点数的表达式初始化 __strong 时,我遇到了一个错误。它在 berekenQuotient 方法中对象 breuk 的初始化时给出。
我还想知道是否有必要在 breuk.h 中声明属性 mijnTeller 和 mijnNoemer,因为它们是通过 berekenQuotient 传递的。发送!
代码:
视图控制器.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
视图控制器.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
布鲁克.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
bruk.m
#import "breuk.h"
@implementation breuk
@synthesize mijnTeller;
@synthesize mijnNoemer;
@synthesize quotient;
- (float)initWithTeller:(float)mijnTeller andNoemer:(float)mijnNoemer{
return quotient = mijnTeller/mijnNoemer;
}
@end