0

我检查了几个建议的链接,但没有找到答案。我想将两个浮点值传递给对象 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
4

3 回答 3

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
于 2013-10-21T14:44:51.917 回答
1

所有init方法都应该返回它自己的类的实例,你想要的只是一个进行计算的类方法。此外,所有类都应以大写字母开头:

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

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

或者只是使用一个类方法

布鲁克.h

  #import <Foundation/Foundation.h>

  @interface Breuk : NSObject

  + (float) quotientWithTeller:(float)teller andNoemer:(float)noemer;
   @end

布鲁克.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];
于 2013-10-21T14:40:03.517 回答
0

使用不兼容类型 float 的表达式初始化 __strong 时出现错误,因为initWithTeller:andNoemer:返回一个浮点数,但您将其分配给breuk1类型为 的breuk。默认情况下,像这样的局部变量breuk1引用,这就是错误的 __strong 部分出现的地方。

您不必breuk.h中声明属性mijnTeller和。mijnNoemer仅当您要存储这些值并稍后再次使用它们时,您才需要这样做。

我也认为您不需要首先尝试创建breuk对象,您可以使用类方法来执行该计算。

于 2013-10-21T14:45:11.870 回答