“NSInvalidArgumentException'的,理由是:”无法解析约束格式“ [英] 'NSInvalidArgumentException', reason: 'Unable to parse constraint format'

查看:311
本文介绍了“NSInvalidArgumentException'的,理由是:”无法解析约束格式“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要旋转屏幕期间保持停止子视图,所以我决定把NSLayoutConstraint类型:



尾随空格来的SuperView

顶层空间,以上海华

按钮空间来的SuperView


我在的UITableViewCell的子类。我写了code,但我得到以下错误:

 'NSInvalidArgumentException'的,理由是:无法解析约束格式:
 自不在次字典的关键。
 H:[self.arrows] -5- |



在CustomCell.m我的code是:

  self.arrows = [[箭头的alloc] initWithFrame:方法CGRectMake(self.contentView.bounds.size.width-30,self.bounds.origin.y + 4,30,自.contentView.bounds.size.height-4)]; *的NSDictionary = viewsDictionary NSDictionaryOfVariableBindings(self.arrows,self.contentView);
 NSMutableArray里*约束= [[NSMutableArray里的alloc]初始化];
 [约束addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@H:[self.arrows] -5 |选项​​:0的指标:无意见:viewsDictionary]];
 [约束addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@V:| -1- [self.arrows]选项:0的指标:无意见:viewsDictionary]];
 [约束addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@[V:[self.arrows] -1- |选项​​:0的指标:无意见:viewsDictionary]];
 [self.arrows addConstraints:约束];


解决方案

它看起来像的自动布局可视化格式解析引擎是间preting了在VFL约束像它的使用是一个的keyPath而不是关键 valueForKeyPath:

NSDictionaryOfVariableBindings(...)将采取一切你的参数是在括号,并把它翻译成与该对象的值字面键(在您的情况: @ {self.arrow:self.arrow} )。在VFL的情况下,自动布局在想,你有一个关键的名为在你看来字典使用具有<$ C $的关键一子字典(或子对象) C>箭头,

  @ {
   @自我:@ {@箭头:self.arrow}
}

当你从字面上希望系统间preT你的密钥为 self.arrow

通常,当我使用一个实例变量吸气剂这样的,我通常最终会创造我自己的字典,而不是使用 NSDictionaryOfVariableBindings(...),如下所示:

 的NSDictionary *意见= @ {@arrowView:self.arrow}

 的NSDictionary *意见= NSDictionaryOfVariableBindings(_arrow);

这将允许您使用您的VFL认为没有自我,你还知道你在说什么:

 的NSArray * arrowHorizCons​​traints = [NSLayoutConstraint constraintsWithVisualFormat:@H:[arrowView] -5 |选项​​:0的指标:无意见]。

 的NSArray * arrowHorizCons​​traints = [NSLayoutConstraint constraintsWithVisualFormat:@H:[_箭头] -5 |选项​​:0的指标:无意见]。

作为一般规则,我已经学会不要有字典键用点(),在其中,以避免任何混乱的系统或调试的噩梦。

I have a subview that I want to keep stops during rotating screen, so I decided to put the NSLayoutConstraint type:

Trailing Space to Superview
Top Space to Superview
Button Space to Superview
I'm in a subclass of UITableViewCell. I wrote the code but I get the following error:

'NSInvalidArgumentException', reason: 'Unable to parse constraint format: 
 self is not a key in the views dictionary. 
 H:[self.arrows]-5-|


My code in CustomCell.m is:

 self.arrows = [[Arrows alloc]initWithFrame:CGRectMake(self.contentView.bounds.size.width-30, self.bounds.origin.y+4, 30, self.contentView.bounds.size.height-4)];

 NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(self.arrows, self.contentView);
 NSMutableArray * constraint=[[NSMutableArray alloc]init];
 [constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:  [self.arrows]-5-|" options:0 metrics:nil views:viewsDictionary]];
 [constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-1-[self.arrows]" options:0 metrics:nil views:viewsDictionary]];
 [constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"[V: [self.arrows]-1-|" options:0 metrics:nil views:viewsDictionary]];
 [self.arrows addConstraints:constraint];

解决方案

It looks like that the autolayout visual format parsing engine is interpreting the "." in your VFL constraint to be a keyPath instead of a key like it's using valueForKeyPath:.

NSDictionaryOfVariableBindings(...) will take whatever your parameter is in the parenthesis and translate it into a literal key with the object as the value (in your case: @{"self.arrow" : self.arrow}). In the case of the VFL, autolayout is thinking that you have a key named self in your view dictionary with a subdictionary (or subobject) that has a key of arrow,

@{
   @"self" : @{ @"arrow" : self.arrow }
}

when you literally wanted the system to interpret your key as "self.arrow".

Usually, when I'm using a instance variables getter like this, I typically end up creating my own dictionary instead of using NSDictionaryOfVariableBindings(...) like so:

NSDictionary *views = @{ @"arrowView" : self.arrow }

or

NSDictionary *views = NSDictionaryOfVariableBindings(_arrow);

Which would allow you to use the view in your VFL without the self and you still know what you're talking about:

NSArray *arrowHorizConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[arrowView]-5-|" options:0 metrics:nil views];

or

NSArray *arrowHorizConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[_arrow]-5-|" options:0 metrics:nil views];

As a general rule, I've learned not to have dictionary keys with a dot (.) in them to avoid any system confusion or debugging nightmares.

这篇关于“NSInvalidArgumentException'的,理由是:”无法解析约束格式“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆