Iphone 无法将浮点分配给NSNumber
我的根模型的m文件中有以下代码:Iphone 无法将浮点分配给NSNumber,iphone,objective-c,xcode4,Iphone,Objective C,Xcode4,我的根模型的m文件中有以下代码: -(id)init { if(self == [super init]) { self.rPrices = [[NSMutableArray alloc]init]; self.rPrices = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil]; } return self; } -(void)saveData:(NSMutableData *)dat
-(id)init {
if(self == [super init]) {
self.rPrices = [[NSMutableArray alloc]init];
self.rPrices = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
}
return self;
}
-(void)saveData:(NSMutableData *)data toFile:(NSString *)file {
float nR4;
nR4 = (some calculations ......)
[self.rPrices addObject:[[NSNumber alloc] numberWithFloat:nR4]];
}
尝试添加对象时出现以下错误:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSPlaceholderNumber WithFloat::]:发送到实例的选择器无法识别
谢谢
或
看起来,您正在对对象调用一个
类方法
尝试更改以下语句
[self.rPrices addObject:[NSNumber numberWithFloat:nR4]];
此外,还可以尝试改变构建数组的方式。”
self.rPrices = [[NSMutableArray alloc] initWithCapacity:2];
[self.rPrices addObjectsFromArray:[NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil]];
numberWithFloat
是一种类方法,因此必须像这样使用它:
[self.rPrices addObject:[NSNumber numberWithFloat:nR4]];
但是,这不起作用,因为您已经为
rplices
属性分配了一个不可变的NSArray
(不可变意味着您不能修改它)。您需要在此处使用NSMutableArray
。您不需要两次初始化数组:
-(id)init {
self = [super init];
if (self != nil) {
//self.rPrices = [[NSMutableArray alloc]init]; //this does not need
rPrices = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", nil];
}
return self;
}
-(void)saveData:(NSMutableData *)data toFile:(NSString *)file {
float nR4;
nR4 = (some calculations ......)
[self.rPrices addObject:[NSNumber numberWithFloat:nR4]];//This return an autoreleased OBJ so you don't need to call alloc
}
您无法将
self
分配给[super init]
返回的对象。它应该是((self=[super init])
。双括号表示该赋值的含义。或者,如果您更喜欢self=[super init];如果(self){…}
也在初始化一个NSArray
,而不是NSMutableArray
,那么当saveData
试图添加一个对象时,它会抛出一个异常。你说得对。。。我只关注了NSNumber的问题。我想你的意思是[[NSNumber alloc]initWithFloat:nR4]
或干脆rPrices=[[NSMutableArray alloc]initWithObjects:@“1”、“2”、“3”、“4”、“nil]代码>(不鼓励在初始值设定项中使用访问器方法)。感谢您的回复。在根模型中,我尝试了:[self.rPrices replaceObjectAtIndex:0 with object:[NSNumber numberWithFloat:(float)nR4]];并修复了初始化,它成功了。但是当我尝试在viewcontroller中检索值时,如下所示:RootModel*rm=[RootModel sharedModel];R4.text=[NSString stringWithFormat:@“%.2f”,[rm.rPrices objectAtIndex:0]];我总是得到0.00。实际上,下面的NSLog也显示0。[self.rPrices replaceObjectAtIndex:0 with object:[NSNumber numberWithFloat:(float)nR4];NSLog(@“%.2f”,[self.rplices objectAtIndex:0]);作为旁注,您没有将self
分配给[super init]
返回的对象。它应该是((self=[super init])
。双括号表示该赋值的含义。或者,如果您更喜欢self=[super init];if(self){…}
。另外,如果rplices
是一个retain/copy属性,则语句self.rplices=[[NSMutableArray alloc]init]代码>泄漏。此外,不鼓励在初始值设定项中使用访问器方法。此外,您正在初始化rplices
两次,这是毫无意义的。将其更改为rPrices=[[NSMutableArray alloc]initWithObjects:@“1”、“2”、“3”、“4”、nil]
。我希望我能对@albertamg在代码中发现的每一个bug将他的评论提高1个百分点。@Perception你的评论对我来说比那些提高投票更有价值:)
[self.rPrices addObject:[NSNumber numberWithFloat:nR4]];
-(id)init {
self = [super init];
if (self != nil) {
//self.rPrices = [[NSMutableArray alloc]init]; //this does not need
rPrices = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", nil];
}
return self;
}
-(void)saveData:(NSMutableData *)data toFile:(NSString *)file {
float nR4;
nR4 = (some calculations ......)
[self.rPrices addObject:[NSNumber numberWithFloat:nR4]];//This return an autoreleased OBJ so you don't need to call alloc
}