objective-c - NSDictionary中的莫名错误

浏览:26日期:2023-12-17

问题描述

// NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(self.segmentControl); NSDictionary * viewsDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:self.segmentControl ,@'segmentControl', nil]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@'V:|-[segmentControl]' options:0 metrics:nil views:viewsDictionary]];// 第一句显示错误,运行不通过,显示不合法的key,原因如下:// terminating app due to uncaught exception ’NSInvalidArgumentException’, reason: ’Unable to parse constraint format: segmentControl is not a key in the views dictionary.

我想知道,为什么第一句不对,而第二句对的。

问题解答

回答1:

NSDictionary * viewsDictionary = @{@'segmentControl':self.segmentControl};[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@'V:|-[segmentControl]' options:0 metrics:nil views:viewsDictionary]];

因为views中的字典是解释-[segmentControl]是哪个视图

你的命名有严重的错误,[self.view addConstraints View上面添加控制器进行约束???

相关文章: