2010-06-20 24 views

Respuesta

176

El enfoque KVC se parece a esto:

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

o

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

con los números como un NSArray

+1

Gracias, estaba tratando de usar valueForKeyPath pero no estaba usando .intValue por lo que no estaba funcionando. –

+0

¿Qué sucede si algunos elementos de NSArray son NSNulls? http://stackoverflow.com/questions/4499109/how-to-tell-nsarray-valueforkeypath-to-ignore-nsnulls – HiveHicks

+14

De hecho, sería mejor usar '@" @ max.self "'. Como '@ max' usa' compare: ', necesita objetos reales: con' intValue', no solo pierde precisión, sino que 'valueForKeyPath:' tiene que volver a crear NSNumbers para trabajar. Como beneficio adicional, también funciona con 'NSString's o cualquier cosa que implemente' compare: '. –

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

// Maximum = 67 
0

la esperanza será útil para usted.

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); 
Cuestiones relacionadas