0

I'm Using Swift 3 and firebase to create a social app where users can make posts but the thing is that I don't want the users to use curse words.

Is there a way like to replace these words when the users writes them?
Currently the users use a text view to write the things they want to post...

How can I do it in like the easiest and simple way?

I was looking for answers and I find this question Swift: how to censor/filter text entered for swear words, etc?.
With this answer code:

import Foundation

func containsSwearWord(text: String, swearWords: [String]) -> Bool {
return swearWords
    .reduce(false) { $0 || text.contains($1.lowercased()) }
}

// example usage
let listOfSwearWords = ["darn", "crap", "newb"] 
    /* list as lower case */

let userEnteredText1 = "This darn didelo thread is a no no."
let userEnteredText2 = "This fine didelo thread is a go."

print(containsSwearWord(text: userEnteredText1, swearWords: listOfSwearWords)) // true
print(containsSwearWord(text: userEnteredText2, swearWords: listOfSwearWords)) // false

But I don't really get it. How can I implement that in my project? Where should I paste that code? Or how can I link that code to my text view? And what it´s missing on that code to work as it should be?

Community
  • 1
  • 1
killerwar557
  • 29
  • 1
  • 7

2 Answers2

0

If you use UITextField on .editingChanged event.

 inputTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)

 func textFieldDidChange(_ textField: UITextField) {
        print(containsSwearWord(text: textField, swearWords: listOfSwearWords)) // Here you check every text change on input UITextField
}

UPDATE

You must implement UITextViewDelegate (Official Documentation)

Validate before input ends

In textViewShouldEndEditing(_ textView: UITextView) validate user input and return true/false to allow editing end.

Validate after input ends

In textViewDidEndEditing(_ textView: UITextView) validate user input and show any warning if you found not allowed words.

sergiog90
  • 104
  • 1
  • 5
0

This is how I would do it.

  • Pass TextView text to NSString
  • Convert NSString to NSArray
  • Check to see if that NSArray contains one of your curse string. If it does then do replace it with **** or something.

This is how it would look in objective-C.

NSString *textViewStr = myTextView.text;
NSArray *myArray1 = [textViewStr componentsSeparatedByString:@" "]; //this will put all words separated by space into an array
if ( [myArray1 containsObject:@"crap"] ) {
    //found one
    textViewStr = [textViewStr stringByReplacingOccurrencesOfString:@"crap"
                                     withString:@"duck"];
}

while googling I found another better answer.

//If you want multiple string replacement:
NSString *s = @"foo/bar:baz.foo";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"/:."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
NSLog(@"%@", s); // => foobarbazfoo

String replacement in Objective-C

Community
  • 1
  • 1
Sam B
  • 27,273
  • 15
  • 84
  • 121