表达式类型“ @lvalue字符串?”没有更多的上下文是模棱两可的

时间:2019-09-12 07:31:52

标签: ios swift

一无所获,我得到了这个错误表达式类型'@lvalue String?'在我的代码中没有更多上下文会变得模棱两可

    if textView.text != "" && takenImage != nil && userLocation.text != "" {
        checkInternet()
        // Create and save a new job
        let newJob = Job(text: textView.text, jobImage: takenImage!, addedByUser: (userLabel?.text)!(-> ERROR), userImage: UserImage, location: userLocation.text, passMap: takenLocation, userID: userID, postID: key)
        newJob.save()
    } 

我试图用其他问题的答案来解决它,但是我没有使它起作用...

工作班:

var text: String = ""
var location: String = ""
var map: String?
var passMap: String?
var addedByUser: String?
var userImage: UIImage?
var jobImage: UIImage?
var downloadURL: String?
var userDownloadURL: String?
var numberOfLikes = 0
var numberOfDislikes = 0
let ref: DatabaseReference!
var userID: String?
var postID: String?


init(text: String? = nil, jobImage: UIImage? = nil, addedByUser: String? = nil, userImage: UIImage? = nil, location: String? = nil, passMap: String? = nil, userID: String? = nil, postID: String? = nil, data: [String : Any]) {
    self.text = text!
    self.jobImage = jobImage
    self.addedByUser = addedByUser
    self.userImage = userImage
    self.location = location!
    self.passMap = passMap
    self.userID = userID
    self.postID = postID
    ref = Database.database().reference().child("jobs").childByAutoId()
}

2 个答案:

答案 0 :(得分:1)

两件事是错误的:

textView.text != ""  您正在将可选String与非可选if let text = textView.text, text != ""进行比较。

替换为:

self.text = text ?? "" //will avoid crash

然后在您的构造函数中,不要打开文本!

如果要保留可选内容:

init(text: String = "")

或更佳:

DocumentFile documentFile = DocumentFile.fromSingleUri(this, data.getdata()); String fileName = documentFile.getName();

答案 1 :(得分:1)

在上面的代码中,

  1. 直接使用userLabel?.text,无需展开任何内容。 addedByUser的类型为String?

  2. 您正在为UserImage参数使用userImage类型。改用UIImage实例或nil

  3. 您尚未在代码中添加data参数。

这是代码,

let newJob = Job(text: textView.text, jobImage: takenImage, addedByUser: userLabel?.text, userImage: nil, location: userLocation.text, passMap: takenLocation, userID: userID, postID: key, data: [:])

避免不必要地强制展开 optionals。它可能会导致运行时异常