我有一个自定义类 Custom.mm,我在其中尝试使用控制器类 MainController 中的设置器设置浮点值。Custom 实例被键入为 id,因为它是一个 Obj-C++ 文件,并且在编译时指向正确的类对我来说效果很好。一切正常,实例已验证。如果我将数量变量设置为 int 类型并传递 int ,它就可以正常工作。与任何其他值或对象相同——除了浮点数。由于某种原因,浮点数(float、CGFloat 等)在 Custom.mm 类中设置为 0。这不是 NSLog 或任何东西的问题——我已经用断点检查了 amount 变量,一切正常但浮动。
//Custom.h
@interface Custom : UIView
{
CGFloat amount;
}
@property CGFloat amount;
@end
//Custom.mm
@implementation Custom
@synthesize amount;
- (id) initWithCoder:(NSCoder*)coder
{
if ((self = [super initWithCoder:coder]))
{
//set initial value to 1
self.amount = 1.0; //verified as 1.0
}
return self;
}
//MainController.h
@interface MainController : UIViewController
{
IBOutlet id customInstance; //IB points to the CustomView class
}
//MainController.m
@implementation MainController
-(void)viewDidLoad
{
[super viewDidLoad];
//Checking this value in Custom.mm via the debugger shows it as being 0,
//when before it was 1.0.
[customInstance setAmount:2.0];
}
@end