此代码工作良好的原因:
NSArray* arr = @[[CALayer layer], [CALayer layer]];
NSString *sumKeyPath = @"@sum.bounds.size.width";
CGFloat totalSize = [[arr valueForKeyPath:sumKeyPath] floatValue];但是这段代码会产生错误:
NSArray* arr = @[[UIImage imageNamed:@"img1"], [UIImage imageNamed:@"img2"]];
NSString *sumKeyPath = @"@sum.size.width";
CGFloat totalSize = [[arr valueForKeyPath:sumKeyPath] floatValue];Error:NSConcreteValue valueForUndefinedKey::该类对键宽不兼容键值编码。
NSArray* arr = @[[UIView new], [UIView new]];
NSString *sumKeyPath = @"@sum.bounds.size.width";
CGFloat totalSize = [[arr valueForKeyPath:sumKeyPath] floatValue];给出同样的错误
发布于 2013-03-27 11:41:00
CALayer对valueForKeyPath:有一个特殊的实现。例如,以下工作:
CALayer *layer = [CALayer layer];
id x0 = [layer valueForKeyPath:@"bounds"];
// --> NSValue object containing a NSRect
id y0 = [layer valueForKeyPath:@"bounds.size"];
// --> NSValue object containing a NSSize
id z0 = [layer valueForKeyPath:@"bounds.size.width"];
// --> NSNumber object containing a float但下列各点不起作用:
CALayer *layer = [CALayer layer];
id x = [layer valueForKey:@"bounds"];
// --> NSValue object containing a NSRect
id y = [x valueForKey:@"size"];
// --> Exception: '[<NSConcreteValue 0x71189e0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key size.'因此,通常情况下,包含NSValue或NSSize的NSRect对象不符合键值。它只适用于CALayer,因为valueForKeyPath:实现处理整个密钥路径,而不是计算第一个键和传递剩余的密钥路径。
UIImage没有valueForKeyPath:的特殊实现。因此
UIImage *img1 = [UIImage imageNamed:@"img1"];
id x1 = [img1 valueForKey:@"size"];
// --> NSValue containing a NSSize很管用,但是
UIImage *img1 = [UIImage imageNamed:@"img1"];
id x1 = [img1 valueForKeyPath:@"size.width"];不管用。
发布于 2013-03-27 11:06:06
我认为这个错误确切地告诉你那个问题是什么!“此类对键宽不兼容键值编码。”
https://stackoverflow.com/questions/15657156
复制相似问题