2

Do not mark as duplicate, edit of this post has set it apart from the other possible duplicate post. I'm in the process of registering users into my application. It is essential that I disallow as many vulgar usernames as possible as they can be seen by everybody. What I have done thus far is:

  1. Collect proposed username from UITextView
  2. Pull the list of disallowed words from my Firebase database

I am returning the snapshot as its default return type, so, from here, I need to search said snapshot for any resemblance of the proposed username. How can I accomplish this? Here is some of the code I am using below:

 func nameFilter(input : String)-> Bool{
    var profanity : Bool  = false
    let dataRef = FIRDatabase.database().reference()
    dataRef.child("Profanity").observeSingleEventOfType(.Value) { (snap: FIRDataSnapshot) in
        if(snap.exists()){
            if(snap.hasChild(input)){
                print("profanity")
                profanity = true
            }else{
         profanity = false
        }
        }else{
            print("null")
        }
    }
    return profanity
}

This didn't seem to work at all, so does anybody here have any ideas as to what I can try? This function always seems to return false no matter what I pass through it.

Ethan
  • 1,905
  • 2
  • 21
  • 50
  • 1
    If you've stored it as one long string, see [How do I check if a string contains another string in Swift?](http://stackoverflow.com/questions/24034043/how-do-i-check-if-a-string-contains-another-string-in-swift). – Frank van Puffelen Jul 29 '16 at 13:51
  • @FrankvanPuffelen updated information, not saving it as string as that is actually impossible. It is being saved as a list of some sort. How can I search this? – Ethan Jul 29 '16 at 16:31
  • 1
    Is `Profanity = false` supposed to be uppercased? – Seth Jul 29 '16 at 17:14
  • @Seth, no, added that in while typing the post. Has no part in the code right now – Ethan Jul 29 '16 at 17:15
  • Can you add (a representative snippet of) the JSON at `Profanity` as text (no screenshot), which you can export from the Firebase Database console? – Frank van Puffelen Jul 29 '16 at 17:21
  • @FrankvanPuffelen not exactly sure that it would be appropriate to post something like that on here as it is filled with, well, a couple hundred lines of swears. I'll host the file on dropbox for you to take a look at. – Ethan Jul 29 '16 at 18:27
  • @FrankvanPuffelen here the link for the file: https://www.dropbox.com/s/cud3rsmanpih84b/speedster-2c89f-export-5.json?dl=0 – Ethan Jul 29 '16 at 18:36

1 Answers1

1

I took care of this one myself, the code I used to accomplish this was:

func nameFilter(input : String)-> Bool{
    var profanity : Bool = true
    let dataRef = FIRDatabase.database().reference()
    dataRef.child("Profanity").observeSingleEventOfType(.Value) { (snap: FIRDataSnapshot) in
        if(snap.exists()){
            if(snap.value! as! NSArray).containsObject(input){
                print("our ears!")
                profanity = true
            }else{
            profanity = false
        }


    }


}
    return profanity
}

Hopefully it is as useful for other people as it was to be.

Ethan
  • 1,905
  • 2
  • 21
  • 50
  • Okay but how do you handle the case of a string like "assassin" or "class" vs "ass" .contains method will flag all of it – Fallah Apr 04 '17 at 21:00