gpt4 book ai didi

ios - 使用不兼容类型 float 的表达式初始化 "breuk *__strong"

转载 作者:行者123 更新时间:2023-11-28 22:24:01 28 4
gpt4 key购买 nike

我检查了几个建议的链接,但没有找到答案。我想将两个浮点值传递给对象 breuk 的初始化。该对象有一个初始化方法initwithTeller: (float) mijnTeller 和Noemer: (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

布鲁克.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

布鲁克.m

   #import "breuk.h"

@implementation breuk
@synthesize mijnTeller;
@synthesize mijnNoemer;
@synthesize quotient;


- (float)initWithTeller:(float)mijnTeller andNoemer:(float)mijnNoemer{
return quotient = mijnTeller/mijnNoemer;

}

@end

最佳答案

您的 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

关于ios - 使用不兼容类型 float 的表达式初始化 "breuk *__strong",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19497433/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com