HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 使用Swift 3时的’InvalidPathValidation’大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用Firebase在用户之间发送和接收消息的应用.在将我的代码更新到新的 Swift 3以便我可以将iOS 10的酷炫新功能添加到我的应用程序的过程中,我遇到了几个错误.我能够在运行时修复除了这一个之外的大多数:

TerminaTing app due to uncaught exception 'InvalidPathValidation',reason: '(child:) Must be a non-empty String and not contain '.' '#' '$' '[' or ']''

我不知道是什么导致了这一点.用户的userID在登录时会打印到控制台,但是一旦按下登录按钮,我的应用程序就会崩溃.在我更新到Swift 3之前,这个错误是不存在的,实际上只有在我修复了应用程序的另一部分中的错误之后才出现.

无论如何,我的应用程序中只有两个主要类:LoginViewController和ChatViewController.

这是LoginViewController的代码

import UIKit
import Firebase

class LoginViewController: UIViewController {

    // MARK: Properties
    var ref: FIRDatabaseReference! // 1
    var userID: String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        ref = FIRDatabase.database().reference() // 2
    }

    @IBACtion func loginDidTouch(_ sender: AnyObject) {
        FIRAuth.auth()?.signInAnonymously() { (user,error) in
            if let user = user {
                print("User is signed in with uid: ",user.uid)
                self.userID = user.uid
            } else {
                print("No user is signed in.")
            }

            self.performSegue(withIdentifier: "Logintochat",sender: nil)

        }

    }

    override func prepare(for segue: UIStoryboardSegue,sender: AnyObject?) {
        super.prepare(for: segue,sender: sender)
        let navVc = segue.desTinationViewController as! UINavigationController // 1
        let chatVc = navVc.viewControllers.first as! ChatViewController // 2
        chatVc.senderId = userID // 3
        chatVc.senderDisplayName = "" // 4
    }


}

这是我的ChatViewController代码

import UIKit
import Firebase
import JSQmessagesViewController

class ChatViewController: JSQmessagesViewController {

    // MARK: Properties

    var rootRef = FIRDatabase.database().reference()
    var messageRef: FIRDatabaseReference!

    var messages = [JSQmessage]()
    var outgoingBubbleImageView: JSQmessagesBubbleImage!
    var incomingBubbleImageView: JSQmessagesBubbleImage!

    var userIsTypingRef: FIRDatabaseReference! // 1
    private var localTyping = false // 2
    var isTyping: Bool {
        get {
            return localTyping
        }
        set {
            // 3
            localTyping = newValue
            userIsTypingRef.SETVALue(newvalue)
        }
    }
    var usersTypingQuery: FIRDatabaseQuery!


    override func viewDidLoad() {
        super.viewDidLoad()
        title = "ChatChat"
        setupBubbles()
        // No avatars
        collectionView!.collectionViewLayout.incomingAvatarViewSize = CGSize.zero
        collectionView!.collectionViewLayout.outgoingAvatarViewSize = CGSize.zero
        messageRef = rootRef.child("messages")
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        observemessages()
        observeTyping()
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    }


    func collectionView(collectionView: JSQmessagesCollectionView!,messageDataForItemATindexPath indexPath: NSIndexPath!) -> JSQmessageData! {
        return messages[indexPath.item]
    }

    func collectionView(collectionView: JSQmessagesCollectionView!,messageBubbleImageDataForItemATindexPath indexPath: NSIndexPath!) -> JSQmessageBubbleImageDatasource! {
        let message = messages[indexPath.item] // 1
        if message.senderId == senderId { // 2
            return outgoingBubbleImageView
        } else { // 3
            return incomingBubbleImageView
        }
    }

    override func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: int) -> Int {
        return messages.count
    }

    func collectionView(collectionView: JSQmessagesCollectionView!,avatarImageDataForItemATindexPath indexPath: NSIndexPath!) -> JSQmessageAvatarImageDatasource! {
        return nil
    }

    private func setupBubbles() {
        let factory = JSQmessagesBubbleImageFactory()
        outgoingBubbleImageView = factory?.outgoingmessagesBubbleImage(
            with: UIColor.jsq_messageBubbleBlue())
        incomingBubbleImageView = factory?.incomingmessagesBubbleImage(
            with: UIColor.jsq_messageBubbleLightGray())
    }

    func addmessage(id: String,text: String) {
        let message = JSQmessage(senderId: id,displayName: "",text: text)
        messages.append(message!)
    }

    override func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        let cell = super.collectionView(collectionView,cellForItemAt: indexPath) as! JSQmessagesCollectionViewCell

        let message = messages[indexPath.item]

        if message.senderId == senderId {
            cell.textView!.textColor = UIColor.white()
        } else {
            cell.textView!.textColor = UIColor.black()
        }

        return cell
    }

    func didPressSendButton(button: UIButton!,withmessage@R_893_3801@: String!,senderId: String!,senderDisplayName: String!,date: NSDate!) {

        let itemRef = messageRef.childByAutoId() // 1
        let messageItem = [ // 2
            "text": text,"senderId": senderId
        ]
        itemRef.SETVALue(messageItem as? AnyObject) // 3

        // 4
        JSQSystemSoundPlayer.jsq_playmessageSentSound()

        // 5
        finishSendingmessage()

        isTyping = false

    }

    private func observemessages() {
        // 1
        let messagesQuery = messageRef.queryLimited(toLast: 25)
        // 2
        messagesQuery.observe(.childAdded) { (snapshot: FIRDataSnapshot!) in
            // 3
            let id = snapshot.value!["senderId"] as! String
            let text = snapshot.value!["text"] as! String

            // 4
            self.addmessage(id: id,text: text)

            // 5
            self.finishReceivingmessage()
        }
    }

    private func observeTyping() {
        let typingInDicatorRef = rootRef.child("typingInDicator")
        userIsTypingRef = typingInDicatorRef.child(senderId)
        userIsTypingRef.onDisconnectRemoveValue()

        // 1
        usersTypingQuery = typingInDicatorRef.queryorderedByValue().queryEqual(toValue: truE)

        // 2
        usersTypingQuery.observe(.value) { (data: FIRDataSnapshot!) in

            // 3 You're the only typing,don't show the inDicator
            if Data.childrenCount == 1 && self.isTyping {
                return
            }

            // 4 Are there others typing?
            self.showTypingInDicator = data.childrenCount > 0
            self.scrollToBottom(animated: truE)
        }
    }

    override func textViewDidChange(_ textView: UITextView) {
        super.textViewDidChange(textView)
        // If the text is not empty,the user is typing
        isTyping = textView.text != ""
    }

    func collectionView(collectionView: JSQmessagesCollectionView!,attributedTextForCellBottomLabelATindexPath indexPath: NSIndexPath!) -> AttributedString! {
        return AttributedString(String:"test")
    }

}

记住,所有这代码都是用Swift 3编写的.

如果您能找到任何可以帮助我解决问题并最终让我的应用程序运行的东西,我将成为您最好的朋友.

提前致谢!

解决方法

Swift 3.0是目前处于测试阶段的开发者版本,预计将于2016年底发布.

有些库可能尚未提供,或者可能包含错误,因此您应该立即重新安装最新的Swift公共稳定版本(目前编写v2.2).

您自己说您已更新为Xcode的测试版.因此,我建议您通过重新下载Xcode from the Mac App Store删除Xcode的测试版来重新安装Swift 2.2.

对于Swift 3,请等到Apple在2016年秋季发布公开版本.然后,查看您的代码是否有效.

Firebase可能尚未更新其Swift 3.0库.

在此之前,我建议你继续使用Swift 2.2.由于兼容性错误,您不希望您的应用突然停止工作.

你说你下载了Xcode的beta版.我至少会等待Apple在2016年秋季发布Xcode的公开版本,然后再次尝试使用您的代码.

但是,我们不知道Firebase何时会更新其库.保留代码备份,并在备用机器中下载更新版本的Xcode.如果您的代码在备用计算机中正常运行,请在主计算机中下载.或者,安装一个虚拟机,如VirtualBox,然后在那里试用你的应用程序.

大佬总结

以上是大佬教程为你收集整理的ios – 使用Swift 3时的’InvalidPathValidation’全部内容,希望文章能够帮你解决ios – 使用Swift 3时的’InvalidPathValidation’所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。