简体   繁体   English

无法分配“字符串”类型的值? 输入“用户?”

[英]Cannot assign value of type 'String?' to type 'User?'

I try to perform a segue from a button to a specific userID chatController with this code:我尝试使用以下代码执行从按钮到特定用户 ID chatController 的转场:

@IBAction func toChatPressed(_ sender: Any) {
    let user = jobDetails?.userID
    let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewFlowLayout())
    chatLogController.user = user
    navigationController?.pushViewController(chatLogController, animated: true)
}

But its giving me Cannot assign value of type 'String?'但它给了我无法分配“字符串”类型的值? to type 'User?'输入“用户?” as an error...作为错误...

chatController.user: chatController.user:

var user: User? {
    didSet {
        navigationItem.title = user?.name
        observeMessages()
    }
}

I get the userID passed from a tableViewCell and is saved in a structure我从 tableViewCell 获取用户 ID 并保存在结构中

struct JobDetails {
    var jobDetail: String
    var userName: String
    var userImage: UIImage?
    var jobImage: UIImage?
    var downloadURL: String?
    var userLocation: String
    var userID: String?
}

In chatController , the property user is of type User , iechatController ,属性userUser类型,即

var user: User? {
    ....
}

Now, in toChatPressed(_:) method, you're setting the value of userID to chatController's user as evident from your code,现在,在toChatPressed(_:)方法中,您将userID的值设置为chatController's user如您的代码所示,

let user = jobDetails?.userID 

Here userID is a String?这里userID是一个String? . . So, user in the above code is of type String?那么,上面代码中的userString?类型的String?

chatLogController.user = user 

In the above code, you are assigning user (of type String? ) to chatLogController.user (of type User ).在上面的代码中,您将user (类型为String? )分配给chatLogController.user (类型为User )。 This is what your error means.这就是你的错误的意思。

Solution:解决方案:

There can be 2 options to resolve the issue.可以有 2 个选项来解决该问题。

1. Either create a property userID of type String? 1.要么创建一个String?类型的属性userID String? in chatLogController , iechatLogController ,即

var userID: String?

Now, set it from toChatPressed(_:) like,现在,将它从toChatPressed(_:)设置为,

chatLogController.userID = jobDetails?.userID

2. Or, you can create a User object using jobDetails in toChatPressed(_:) and then set it to chatLogController's user , ie 2.或者,您可以使用jobDetails toChatPressed(_:)中的toChatPressed(_:)创建一个User对象,然后将其设置为chatLogController's user ,即

let user = User(jobDetails) //this depends upon the how you've defined User
chatLogController.user = user

In case you still have any confusion, feel free to ask.如果您仍有任何困惑,请随时提问。

You are assigning JobDetails.userID: String?您正在分配JobDetails.userID: String? to ChatLogController.user: User?ChatLogController.user: User?

See your code:查看您的代码:

    let user = jobDetails?.userID
    …
    chatLogController.user = user

You have to map the jobDetails?.userID: String to a User?您必须将jobDetails?.userID: String映射到User? . .

Inside of your struct JobDetails , add a new field containing the User object:struct JobDetails ,添加一个包含User对象的新字段:

struct JobDetails {
    var jobDetail: String
    var userName: String
    var userImage: UIImage?
    var jobImage: UIImage?
    var downloadURL: String?
    var userLocation: String
    var userID: String?
    var user: User? // <-- New field
}

Whenever you create a new JobDetails , pass the User object to the user field.每当您创建新的JobDetails ,将User对象传递给user字段。

Now, you function can use the user field instead of the userID field:现在,您的函数可以使用user字段而不是userID字段:

@IBAction func toChatPressed(_ sender: Any) {
    let user = jobDetails?.user // <-- user of type User? instead of userID of type String?
    let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewFlowLayout())
    chatLogController.user = user
    navigationController?.pushViewController(chatLogController, animated: true)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM