Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 无法将浮点分配给NSNumber_Iphone_Objective C_Xcode4 - Fatal编程技术网

Iphone 无法将浮点分配给NSNumber

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

我的根模型的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 *)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

}