Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 03-11-2010, 02:08 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 67
Default NSString isEqualToString NSMutableString Problem

Hi, I am having problems with comparing an NSString with a string from an array. I used the following one line of code to try and compare the 2 with an if statement:
if ([x isEqualToString:guidString])


Now I am not sure if it is because one is an NSString and one is an NSMutableString or if I am just missing something. (I do know that the array does have the correct information in it).



int totalArrayElements = [arrayArticleGUID count];
int countGuidRow = 0;
NSString *guid = nil;

NSURLRequest *currentRequest = [a2WebView request];
NSURL *currentURL = [currentRequest URL];
NSString *guidString = [currentURL absoluteString];
guidString = [guidString stringByReplacingOccurrencesOfString:@"http://www.google.com/entry.php?id=" withString:@""]; // Compare NSString.

for (int scanArrayElements = 0; scanArrayElements < totalArrayElements; scanArrayElements++) {
NSString *x = [NSString stringWithString: [[arrayArticleGUID objectAtIndex: scanArrayElements] objectForKey: @"guid"]]; // to NSMutableString from Array???.

if ([x isEqualToString:guidString]) { // here is where I am having problem. Cant get if statement to work.
countGuidRow = scanArrayElements;
}
}

guid = [[arrayArticleGUID objectAtIndex: (countGuidRow-1)] objectForKey: @"guid"];

NSString *stringGUID1 = @"http:www.google.com/entry.php?id=";
NSString *stringGUID2 = guid;
stringGUID1 = [stringGUID1 stringByAppendingString:stringGUID2];
NSURL *requestURL = [NSURL URLWithString:stringGUID1];
requestObj = [NSURLRequest requestWithURL:requestURL];
[a2WebView loadRequest:requestObj];

Last edited by scleland; 03-11-2010 at 03:01 PM.
scleland is offline   Reply With Quote
Old 03-11-2010, 04:07 PM   #2 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 67
Default

So if I had a few lines of code:


NSMutableString *mutablestring = [[NSMutableString alloc] init];
NSString *string = nil;

mutablestring = [[array objectAtIndex: 1] objectForKey: @"id"]; // assuming this array element equals "test"
string = @"test";




How do I make this statement TRUE?
if (mutablestring isEqualToString: string)
scleland is offline   Reply With Quote
Old 03-11-2010, 06:14 PM   #3 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

The way you used isEqualToString in your first snippet is good, and it should work even is one or both is a mutable string. I assume it's always returning false? That could happen if one or both string is nil. Did you try logging the strings to see what you're really getting?

Code:
NSLog( @"x is: %@ ", x);
NSLog( @"guidString is: %@ ", guidString);

if ([x isEqualToString:guidString]) {
countGuidRow = scanArrayElements;
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-12-2010, 09:34 AM   #4 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 67
Default

Thank you very much! There was a trailing new line (\n) at the end of all of my array elements. I wasnt checking the log because if I took that array element and used it to load a UIWebView, XCode was ignoring the trailing character. So laziness on my part so I appreciate it a lot!

Another problem came up though that I am looking for the answer for and will post when I find it. When I use:


arrayElementString = [arrayElementString stringByReplacingOccurrencesOfString:@"\n" withString:@""];


... with a NSMutableString, I get the warning:


"incompatible Objective-C types assigning 'struct NSString *', expected 'struct NSMutableString *'"


So when I find the answer I will post it, if anyone knows the function (or method) to use for an NSMutableString, ot would be great! Thank you again.
scleland is offline   Reply With Quote
Old 03-12-2010, 10:11 AM   #5 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 67
Default

So I ended up working around it for now. I created an NSString and made the NSString equal the NSMutableString, and then replaced the characters. I know this isnt good code but I have to move on for now. When/If I find the correct way I will post it on here.

I did find what I think is the correct code though, but it was crashing my program (I think because I was appending all original elements of the array in the loop permanently, which also crashed my program when I tried to write an external loop to first change all of the elements).


[arrayElementString replaceCharactersInRange: [arrayElementString rangeOfString: @"\n"] withString: @""];
scleland is offline   Reply With Quote
Old 03-12-2010, 12:20 PM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Code:
arrayElementString = [arrayElementString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
The method you're using, stringByReplacingOccurrencesOfString, creates a new NSString. It does not modify your mutable string. The compiler error comes because you're pointing your pointer, which is supposed to point to an NSMutableString, at the NSString that gets returned. You might be leaking your mutable string too, since you no longer have a pointer to it.

Look for "replaceOccurrencesOfString: withString: options: range:" , which is an NSMutableString method that actually modifies your mutable string instead of creating a new string.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-13-2010, 05:24 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default

Quote:
Originally Posted by scleland View Post
Thank you very much! There was a trailing new line (\n) at the end of all of my array elements. I wasnt checking the log because if I took that array element and used it to load a UIWebView, XCode was ignoring the trailing character. So laziness on my part so I appreciate it a lot!

Another problem came up though that I am looking for the answer for and will post when I find it. When I use:


arrayElementString = [arrayElementString stringByReplacingOccurrencesOfString:@"\n" withString:@""];


... with a NSMutableString, I get the warning:


"incompatible Objective-C types assigning 'struct NSString *', expected 'struct NSMutableString *'"


So when I find the answer I will post it, if anyone knows the function (or method) to use for an NSMutableString, ot would be great! Thank you again.
The stringByReplacingOccurrencesOfString method takes a string as input, and returns a different string as a result.

If you want to replace strings in a mutable string instead, use the NSMutableString method

replaceOccurrencesOfString:withStringptions:rang e

Instead.


Regards,

Duncan C
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 257
19 members and 238 guests
14DEV, @sandris, ADY, ArtieFufkin10, bookesp, ckgni, Dani77, DarkAn, Desert Diva, HemiMG, iDifferent, jakerocheleau, JasonR, prchn4christ, Rudy, ryantcb, Speed, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:46 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0