Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 将代码从Xcode7.2转换为Xcode7.3时出错_String_Swift_Xcode7.3 - Fatal编程技术网

String 将代码从Xcode7.2转换为Xcode7.3时出错

String 将代码从Xcode7.2转换为Xcode7.3时出错,string,swift,xcode7.3,String,Swift,Xcode7.3,错误消息说从“字符串?!”向下播放“字符串”仅打开可选项;你想用“!!”吗?我将字符串替换为NSArray,但它不起作用。有人知道如何解决这个问题吗 returnFirebaseUrl(self.myRootRef).queryLimitedToLast(1000).observeEventType(FEventType.ChildAdded, withBlock: { (snapshot) in self.showProgressHUD()

错误消息说从“字符串?!”向下播放“字符串”仅打开可选项;你想用“!!”吗?我将字符串替换为NSArray,但它不起作用。有人知道如何解决这个问题吗

returnFirebaseUrl(self.myRootRef).queryLimitedToLast(1000).observeEventType(FEventType.ChildAdded, withBlock: { (snapshot) in

            self.showProgressHUD()

            // 3 codes below are the reason why I got error for.
            let text = snapshot.value["text"] as? String
            let sender = snapshot.value["from"] as? String
            let name = snapshot.value["name"] as? String


            var isOutGoing:Bool

            if sender == GetUserId(){
                isOutGoing = true
            }else{
                isOutGoing = false
            }

            let mediaItem = self.createPhotoItem(Const.S3_URL + text!, isOutgoing: isOutGoing)

            var message:JSQMessage
            if text!.hasSuffix(".jpg"){
                message = JSQMessage(senderId: sender, displayName: name, media: mediaItem)

            }else{
                message = JSQMessage(senderId: sender, displayName: name, text: text)
            }
            self.messages?.append(message)

看起来您还需要展开
snapshot.value

我可能是这样的:

guard let value = snapshot.value, 
    text = value["text"] as? String,
    sender = value["from"] as? String,
    name = value["name"] as? String else {
        fatalError("Oops, one of the values was nil")
}

// here you can use unwrapped text, sender and name

您需要展开snapshot.value,才能将其用作字典。 作为向导,使用“强制展开”是不安全的因为它可能会导致崩溃

您可以尝试以下方法:

    if let valueDictionary = snapshot.value as? Dictionary<String, AnyObject> {
       let text = snapshot.value["text"] as? String //this will still be optional and might need to carefully unwrap
       etc etc
    }
如果让valueDictionary=snapshot.value为?字典{
将text=snapshot.value[“text”]设为?String//这仍然是可选的,可能需要小心地展开
等等
}

我试过了,但没有成功。你知道解决这个问题的方法吗?当你尝试上面的方法时,你会遇到什么错误?我试过了,但没有成功。你知道有什么解决办法吗?我不知道“没用”是什么意思,我没有占卜能力。。。也许你可以告诉我们错误信息是什么?发生了什么而不是应该发生什么?还有,你调试过什么吗?快照的类型是什么?它是可选的还是不可选的?等