gpt4 book ai didi

ios - String is not identical to NSObject 错误,同时在 Swift 中应用约束(视觉约束)

转载 作者:搜寻专家 更新时间:2023-10-31 21:55:49 26 4
gpt4 key购买 nike

import UIKit
import Foundation
class DetailView: UIViewController{
var fullImage:UIImageView?
var nameLabel:UILabel?
var detailLabel:UILabel?

override func viewDidLoad() {
super.viewDidLoad()
fullImage = UIImageView()
nameLabel = UILabel()
detailLabel = UILabel()
self.view.addSubview(fullImage!)
self.view.addSubview(nameLabel!)
self.view.addSubview(detailLabel!)
}

func applyContraints(){
self.view.addSubview(fullImage!)

fullImage?.setTranslatesAutoresizingMaskIntoConstraints(false)
nameLabel?.setTranslatesAutoresizingMaskIntoConstraints(false)
detailLabel?.setTranslatesAutoresizingMaskIntoConstraints(false)

var viewsDict:[String:AnyObject!] = ["imageView": fullImage!, "nameLabel": nameLabel!, "detailLabel": detailLabel!]

var constraints:NSArray

constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[imageView]-10-|",
options: 0,
metrics: nil,
views: viewsDict)
self.view.addConstraints(constraints)



}
}

最佳答案

constraintsWithVisualFormatviews 参数需要一个[NSObject: AnyObject] 类型的字典。

在创建 viewsDict 时,尝试将其声明为 [NSObject: AnyObject]:

var viewsDict: [NSObject: AnyObject] = ["imageView": fullImage!, "nameLabel": nameLabel!, "detailLabel": detailLabel!]

您还必须将 options 参数值从 0 更改为 nil

此外,如果您想摆脱可选值,您可以尝试以下方法:

var fullImage = UIImageView()
var nameLabel = UILabel()
var detailLabel = UILabel()

代替:

var fullImage: UIImageView?
var nameLabel: UILabel?
var detailLabel: UILabel?

然后,对于 viewsDict 声明,您可以删除 !:

var viewsDict: [NSObject: AnyObject] = ["imageView": fullImage, "nameLabel": nameLabel, "detailLabel": detailLabel]

关于ios - String is not identical to NSObject 错误,同时在 Swift 中应用约束(视觉约束),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27707208/

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