2012-08-13 66 views
1

我有一个问题,我可以如何计算我的plist文件中的项目。我想:如何计算plist中的项目

NSString *bundlePathofPlist = [[NSBundle mainBundle]pathForResource:@"Mything" ofType:@"plist"]; 

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:bundlePathofPlist]; 

    NSArray *dataFromPlist = [dict valueForKey:@"some"]; 

    for(int i =0;i<[dataFromPlist count];i++) 
    { 
     //NSLog(@"%@",[dataFromPlist objectAtIndex:i]); 
     [self setTableData:[dataFromPlist count]]; 

    } 

    NSLog(@"%@", tableData); 

但在这行出现错误:

[self setTableData:[dataFromPlist count]]; 

Implicit conversion of 'NSUInteger' (aka 'unsigned int') to 'NSArray *' is disallowed with ARC 

和警告:

Incompatible integer to pointer conversion sending 'NSUInteger' (aka 'unsigned int') to parameter of type 'NSArray *'; 
+0

@Popeye谢谢你删除不必要的[xcode]标签。 – vikingosegundo 2012-08-13 22:41:27

+0

@vikingosegundo即使我删除了它,我也确信它已被Frenck添加回来。 – Popeye 2012-08-14 08:13:26

+0

@Frenck请不要再添加Xcode标签。对于这个问题,这是无关紧要的。它只会产生混乱和噪音。 – vikingosegundo 2012-08-14 11:25:23

回答

2

看起来您的setTableData需要NSArray实例。你需要在一个循环做准备数组前期,然后将其设置一次,像这样:

NSMutableArray *data = [NSMutableArray array]; 
for(int i =0;i<[dataFromPlist count];i++) 
{ 
    //NSLog(@"%@",[dataFromPlist objectAtIndex:i]); 
    [data addObject:[NSNumber numberWithInt:[[dataFromPlist objectAtIndex:i] count]]]; 
} 
[self setTableData:data]; 

这是假设你的setTableData方法需要NSNumber实例包装int组成的数组。

+0

这段代码会给你'count'的'count'实例,它是'count'。当然这不正确? – 2012-08-13 23:54:47

+0

@KevinBallard你是对的,我复制粘贴从OP的片段,而不考虑它的作用。现在应该修复,谢谢! – dasblinkenlight 2012-08-14 02:39:49

0

问题是你使用[dataFromPlist count]里面的for循环,这使得没有意义。你可能意思是[dataFromPlist objectAtIndex:i]

或者,更地道,

for (NSArray *elt in dataFromPlist) { 
    [self setTableData:elt]; 
} 

虽然我不得不怀疑,为什么你叫-setTableData:一遍又一遍用不同的元素。