2012-08-13 33 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:一遍又一遍用不同的元素。