ios - '常数不是有限的!那是非法的。常量 :nan' error in Objective C

标签 ios objective-c

当我尝试启动我正在使用的应用程序时收到以下错误。 '常数不是有限的!那是非法的。常量:nan'

这是 Instagram 的学校项目克隆。我认为错误出在 subview 中或可能在创建的约束中,但不幸的是我只是没有看到问题。

- (void) layoutSubviews {
    [super layoutSubviews];

     // Before layout, calculate the intrinsic size of the labels (the size they "want" to be), and add 20 to the height for some vertical padding.
    CGSize maxSize = CGSizeMake(CGRectGetWidth(self.bounds), CGFLOAT_MAX);
    CGSize usernameLabelSize = [self.usernameAndCaptionLabel sizeThatFits:maxSize];
    CGSize commentLabelSize = [self.commentLabel sizeThatFits:maxSize];

    self.usernameAndCaptionLabelHeightConstraint.constant = usernameLabelSize.height + 20;
    self.commentLabelHeightConstraint.constant = commentLabelSize.height + 20;
    self.imageHeightConstraint.constant = self.mediaItem.image.size.height / self.mediaItem.image.size.width * CGRectGetWidth(self.contentView.bounds);

    // Hide the line between cells
    self.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(self.bounds));
}

NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(_mediaImageView, _usernameAndCaptionLabel, _commentLabel);

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_mediaImageView]|" options:kNilOptions metrics:nil views:viewDictionary]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_usernameAndCaptionLabel]|" options:kNilOptions metrics:nil views:viewDictionary]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_commentLabel]|" options:kNilOptions metrics:nil views:viewDictionary]];

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_mediaImageView][_usernameAndCaptionLabel][_commentLabel]" options:kNilOptions metrics:nil views:viewDictionary]];



    self.imageHeightConstraint = [NSLayoutConstraint constraintWithItem:_mediaImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100];
    self.imageHeightConstraint.identifier = @"Image height constraint";

    self.usernameAndCaptionLabelHeightConstraint = [NSLayoutConstraint constraintWithItem:_usernameAndCaptionLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100];
    self.usernameAndCaptionLabelHeightConstraint.identifier = @"Username and caption label height constraint";

    self.commentLabelHeightConstraint = [NSLayoutConstraint constraintWithItem:_commentLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100];
    self.commentLabelHeightConstraint.identifier = @"Comment label height constraint";

    [self.contentView addConstraints:@[self.imageHeightConstraint, self.usernameAndCaptionLabelHeightConstraint, self.commentLabelHeightConstraint]];


}

最佳答案

我只看到一个地方你在做除法:

self.imageHeightConstraint.constant = self.mediaItem.image.size.height / self.mediaItem.image.size.width * CGRectGetWidth(self.contentView.bounds);

在这一行打个断点,看看self.mediaItem.image.size是什么样子的。

关于ios - '常数不是有限的!那是非法的。常量 :nan' error in Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30157028/

相关文章:

ios - ios 中的电子邮件和密码验证

ios - 为图像使用单独的包

ios - 从应用程序调用命令行实用程序

ios - 添加弹出窗口时出现 NSInternalInconsistencyException

ios - 将 Swift 字符串转换为数据时出现 EXC_BAD_ACCESS

iphone - 从 UIAlertView 获取文本

iphone - respondsToSelector 总是失败

iphone - UIPickerView 和 UITextField 从 UIPickerView 获取值

ios - TableView 数据源和委托(delegate)方法调用而不从 Web 服务加载数据?

iphone - 如何将 NSArray 中的对象放入 NSSet 中?