views:

302

answers:

2

Hi,

My code:

stringFromRecievedData =
    [[NSString alloc]initWithData:_data1 encoding:NSUTF8StringEncoding];

if (![stringFromRecievedData isEqualToString:lastStringFromRecievedData]) {
    [lastStringFromRecievedData setString: stringFromRecievedData];

I get the same "not equal" result even in the second round- even when it is the same data each time...

Is this the correct way to compare the two?

Thanks.

+4  A: 

If lastStringFromRecievedData is an instance of NSString (or subclass such as NSMutableString), then this is the correct way to compare two strings. In this case something else may be causing your strings to not be equal.

However, it looks like lastStringFromRecievedData is a some other object because you are calling setString: on it. If this is the case, you need to get the string from that object first.

stringFromRecievedData =
    [[NSString alloc]initWithData:_data1 encoding:NSUTF8StringEncoding];

// Assuming 'string' is the function to get the string from 'setString:' below
NSString *otherString = [lastStringFromRecievedData string];

if (![stringFromRecievedData isEqualToString:otherString]) {
    [lastStringFromRecievedData setString: stringFromRecievedData];
Colin Gislason
lastStringFromRecievedData is NSMutableString- So you suggest using [lastStringFromRecievedData string]?
Tiger
No, if it is an `NSMutableString` then the comparison should work. What kind of data is in these strings?
Colin Gislason
The Data is from a web response, The only way I get this to work is if I am using a scanner on the received string to scan it into a new string-in that case the comparison succeeds.I thought there is something about the string format?
Tiger
Maybe I will try using absoluteString on the recieved string?[lastStringFromRecievedData absoluteString]
Tiger
Does logging the two strings shows that they are the same?
Colin Gislason
Yes- at log they are exactly the same.This is also why I suspect that there is somthing to do with the fact that this string is created from Data.[[NSString alloc]initWithData:_data1 encoding:NSUTF8StringEncoding];_data1 is just the data I recieve as a responce to connection request.I tried many things, the only thing that worked for me was to scan the string into a new string using a scanner.Maybe this is what I will do- But I hoped to understand this issue...
Tiger
What happens if you loop through all the characters in each string and compare the individual characters? (using `characterAtIndex:`)
Colin Gislason
Thank you- I have found that there is a difference between them- It was 1 different char in a very long string so I have not seen it...
Tiger
A: 

If all you're doing is looking to make sure your NSMutableString is equivalent to another NSString, why not do something like:

lastStringFromRecievedData = [stringFromRecievedData mutableCopy];

Or:

[lastStringFromRecievedData setString:stringFromRecievedData];

The end result will be the same.

Dave DeLong
What you wrote is exactly what I did from the first-place.Re-read the question - The issue is when I recreate stringFromRecievedData and try to find out if they are equal-If they are not- it means I recieved new Data.Both strings are Identical on the log- but I get a resault that they not equal- this is the issue.
Tiger
I am getting a String from Data and my purpose is to find out if I already got this string before or not.
Tiger