gpt4 book ai didi

ios - 'NSInvalidArgumentException',原因 : 'Unable to parse constraint format'

转载 作者:IT王子 更新时间:2023-10-29 07:36:37 26 4
gpt4 key购买 nike

我有一个 subview ,我想在旋转屏幕期间保持停止,所以我决定放置 NSLayoutConstraint 类型:

到Superview的尾随空格
顶层空间到Superview
按钮空间到Superview
我在 UITableViewCell 的子类中。我写了代码,但出现以下错误:

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


我在 CustomCell.m 中的代码是:

 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];

最佳答案

看起来自动布局视觉格式解析引擎正在将 VFL 约束中的“.”解释为 keyPath 而不是像使用 valueForKeyPath: 那样的键.

NSDictionaryOfVariableBindings(...) 将采用括号中的任何参数并将其转换为以对象为值的文字键(在您的情况下:@{"self .arrow": self.arrow}).在 VFL 的情况下,自动布局认为您在 View 字典中有一个名为 self 的键,其中一个子字典(或子对象)的键为 arrow

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

当您确实希望系统将您的 key 解释为“self.arrow”时。

通常,当我像这样使用实例变量 getter 时,我通常最终会创建自己的字典,而不是像这样使用 NSDictionaryOfVariableBindings(...):

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

NSDictionary *views = NSDictionaryOfVariableBindings(_arrow);

这将允许您在没有 self 的情况下使用 VFL 中的 View ,并且您仍然知道自己在说什么:

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

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

作为一般规则,我学会了不要使用带点 (.) 的字典键,以避免任何系统混淆或调试噩梦。

关于ios - 'NSInvalidArgumentException',原因 : 'Unable to parse constraint format' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13045178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com