2010-06-20 9 views

risposta

176

L'approccio KVC assomiglia a questo:

int max = [[numbers valueForKeyPath:@"@max.intValue"] intValue]; 

o

NSNumber * max = [numbers valueForKeyPath:@"@max.intValue"]; 

con i numeri come NSArray

+1

Grazie, stavo cercando di usare valueForKeyPath ma non stavo usando .intValue quindi non funzionava. –

+0

Cosa succede se alcuni elementi di NSArray sono NSNulls? http://stackoverflow.com/questions/4499109/how-to-tell-nsarray-valueforkeypath-to-ignore-nsnulls – HiveHicks

+14

In effetti, sarebbe meglio usare '@" @ max.self "'. Poiché '@ max' usa' compare: ', ha bisogno di oggetti reali: con' intValue', non solo perdi precisione, ma 'valueForKeyPath:' deve ricreare NSNumbers con cui lavorare. Come bonus, funziona anche con 'NSString's o qualsiasi cosa che implementa' compare: '. –

1
NSArray * test= @[@3, @67, @23, @67, @67]; 
int maximumValue = [[test valueForKeyPath: @"@max.self"] intValue]; 
NSLog(@" MaximumValue = %d", maximumValue); 

// Maximum = 67 
0

Speranza sarà utile a voi.

NSArray * arrayOfBarGraphValues = @[@65, @45, @47 ,@87 , @46, @66 ,@77 ,@47 ,@79 ,@78 ,@87 ,@78 ,@87 ]; 
int maxOfBarGraphValues = [[arrayOfBarGraphValues valueForKeyPath: @"@max.self"] intValue]; 
NSLog(@" MaximumValue Of BarGraph = %d", maxOfBarGraphValues); 
Problemi correlati