COMPARING 2 STRINGS

I am trying to compare two strings and then " do something". The first string is a string that is captured in UITextView using the following code:

**********************************

func updateIncomingData ()

{

NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "Notify"), object: nil , queue: nil)

{

notification in

let appendString = "\n"

let myFont = UIFont(name: "Helvetica Neue", size: 30.0)

let myAttributes2 = [NSFontAttributeName: myFont!, NSForegroundColorAttributeName: UIColor.red]

let attribString = NSAttributedString(string: "" + (characteristicASCIIValue as String) + appendString, attributes: myAttributes2)

let newAsciiText = NSMutableAttributedString(attributedString: self.consoleAsciiText!)

self.password_P_F.attributedText = NSAttributedString(string: characteristicASCIIValue as String , attributes: myAttributes2)

newAsciiText.append(attribString)

self.consoleAsciiText = newAsciiText

self.password_P_F.attributedText = self.consoleAsciiText

let pwmatch:String = "The passwords match"

let pwin:String = self.password_P_F.text

/

print("INPUT PASSWORD" + pwin)

print("COMPARE TO" + pwmatch)

if(pwin == "The passwords match" )

{

print("NOW HERE IT IS")

}

if (pwin == pwmatch)

{

print("at the passwords match")

/

}

}

********************************************************


The debugger shows the incoming string as "Value Received: The passwords match". The following is the debugger window.

****************************

Value Recieved: The passwords match

INPUT PASSWORDThe passwords match

The passwords match

COMPARE TOThe passwords match

*************************

The compare statement "if(pwin == "The passwords match" )" does not return a true but if I type in "The passwords match" instead of using "self.password_P_F.text", the compare statement returns TRUE.

There must be something in the string "self.password_P_F.text" that is different but I cannot figure out what it is.


Any help appreciated.


Thanks

Bill

Compare this code:


            print("INPUT PASSWORD" + pwin)
            print("COMPARE TO" + pwmatch)


with the log output you say it produces:


INPUT PASSWORDThe passwords match
The passwords match
COMPARE TOThe passwords match


That suggests the value of 'pwin' is "The passwords match\nThe passwords match", which isn't what you want or expect.


The earlier code does a lot of messy appending, and I suspect you've accidentally duplicated the original text. Try stepping through the code in the debugger and find the place where it goes wrong.

Did you check that you didn't post notification twice ?


Anyway, the code, even thorugh very short, it is a total mess, impossible to follow. You should reorgnaize an dsimplify seriously


There could be a problem in the following sequence:

           let newAsciiText = NSMutableAttributedString(attributedString: self.consoleAsciiText!)
           self.password_P_F.attributedText = NSAttributedString(string: characteristicASCIIValue as String , attributes: myAttributes2)
           newAsciiText.append(attribString)
           self.consoleAsciiText = newAsciiText


You initialize newAsciiText with self.consoleAsciiText

What is the content of self.consoleAsciiText at that time ?

Then you append a string (with the linefeed): what is newAsciiText at that time.

COMPARING 2 STRINGS
 
 
Q