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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-12-2011, 03:14 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 20
ossama is on a distinguished road
Angry Comparing NSString

Hi

i have tried all my best but just cant get to a solution

I would like to compare incoming socket against i.e @"ack"

here is the code

the incoming string:

NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

compare to:

NSString *str1 = @"ack";

comparison code:

if ([str isEqualToString:str1]) {
label.text = str;
}

when I nslog(str,nil) it shows that it equals ack

but when i compare it to ack it doesn't matchhhh

any ideas please
ossama is offline   Reply With Quote
Old 01-12-2011, 04:07 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

There's no need to post the same question multiple times.

Are they exact in regards to case?
baja_yu is offline   Reply With Quote
Old 01-12-2011, 04:12 PM   #3 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Just ran this and it worked ok:

Code:
	NSString *str1 = @"ack";
	NSData *data = [str1 dataUsingEncoding:NSASCIIStringEncoding];
	NSString *str2 = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
	if ([str2 isEqualToString:@"ack"]) {
		NSLog(@"It's equal");
	}
	[str2 release];
baja_yu is offline   Reply With Quote
Old 01-13-2011, 02:53 AM   #4 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 20
ossama is on a distinguished road
Unhappy

Thanks mate, but i tried it already and not working

the strange thing is that if i convert str to integer it works as highlighted below

here is all the code
Code:
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
    
    switch(eventCode) {
        case NSStreamEventHasBytesAvailable:
        {
            if (data == nil) {
                data = [[NSMutableData alloc] init];
            }
            uint8_t buf[1024];
            unsigned int len = 0;
            len = [(NSInputStream *)stream read:buf maxLength:1024];
            if(len) {    
                [data appendBytes:(const void *)buf length:len];
                int bytesRead;
                bytesRead += len;
            } else {
                NSLog(@"No data.");
            }
           
            NSString *str = [[NSString alloc] initWithData:data 
												  encoding:NSASCIIStringEncoding];
			NSLog(str,nil);
						
			if ([str isEqualToString:@"ack"]) {
				NSLog(@"It's equal");
			label.text = str;
			}
			
			else {
				label.text = @"";
				txtMessage.backgroundColor = [UIColor clearColor];
				txtMessage.text = str; 
			}
				
This below works by the way
				int x = [str intValue];
				if (x > 30)
				{
					//txtMessage.text = @"High";  
					label.text = @"High temperature";
					txtMessage.backgroundColor = [UIColor redColor];
					txtMessage.text = str; 
					
				}
			           
			[str release];
            [data release];        
            data = nil;
			 
        } break;
    }
}
ossama is offline   Reply With Quote
Old 01-13-2011, 03:05 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 159
bluemonkey is on a distinguished road
Default

What's the output of NSString *str = [[NSString alloc] initWithData:data

If you do NSLog(@"output = %@", str);
__________________
Aaron
bluemonkey is offline   Reply With Quote
Old 01-13-2011, 04:42 AM   #6 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 20
ossama is on a distinguished road
Default

2011-01-13 21:42:17.765 Socket[57886:207] output = ack
ossama is offline   Reply With Quote
Old 01-13-2011, 05:51 AM   #7 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

strange, isEqualToString should solve problems about encoding, are you sure that there isn't strange characters? maybe a space, or something other in str, maybe terminator character \0?
__________________

Last edited by dany_dev; 01-13-2011 at 05:56 AM.
dany_dev is offline   Reply With Quote
Old 01-13-2011, 06:02 AM   #8 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Like Dany88 said, check for non-visible characters. What is the output of
Code:
NSLog(@"output = %@ and length = %d", str, str.length);
We know the length of @"ack" should be 3.
RLScott is offline   Reply With Quote
Old 01-13-2011, 06:03 AM   #9 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 20
ossama is on a distinguished road
Default

It is possible that the server is sending ack with the /n
Not too sure
If it is, what should I try
@"ack/n" ??
ossama is offline   Reply With Quote
Old 01-13-2011, 06:08 AM   #10 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 20
ossama is on a distinguished road
Default

2011-01-13 23:05:46.633 Socket[58689:207] output = ack
and length = 4

Thanks for your help

does it mean there is a terminating character \0
any ideas how to compare against that.

ossama is offline   Reply With Quote
Old 01-13-2011, 06:17 AM   #11 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by ossama View Post
2011-01-13 23:05:46.633 Socket[58689:207] output = ack
and length = 4

Thanks for your help

does it mean there is a terminating character \0
any ideas how to compare against that.

Did you post "and length = 4" on a separate line deliberately, or did the newline just appear? It looks like there is a newline character in your data. That makes sense if it came from a text file.
RLScott is offline   Reply With Quote
Old 01-13-2011, 06:23 AM   #12 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

str = [str stringByReplacingOccurancesOfString:@"\n" withString:@""];
__________________
dany_dev is offline   Reply With Quote
Old 01-13-2011, 07:29 AM   #13 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 20
ossama is on a distinguished road
Default

no, the new line just appeared. not deliberate which means there is a carriage return

I tried
Code:
str = [str stringByReplacingOccurancesOfString:@"\n" withString:@""];
but i get an error and the app quits after trying to connect to server

Quote:
warning: 'NSString' may not respond to '-stringByReplacingOccurancesOfString:withString:'
ossama is offline   Reply With Quote
Old 01-13-2011, 07:34 AM   #14 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 20
ossama is on a distinguished road
Default

this seems to have fixed the problem I believe, I will test this later, getting very late now

Code:
if ([str isEqualToString:@"ack\n"])
ossama is offline   Reply With Quote
Old 01-13-2011, 07:43 AM   #15 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 20
ossama is on a distinguished road
Default

Thanks for your time and help.
if you ever need help, yell at me
ossama is offline   Reply With Quote
Reply

Bookmarks

Tags
compare, nsstream, nsstring

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: 367
9 members and 358 guests
7twenty7, blueorb, iAppDeveloper, iGamesDev, Mah6447, Morrisone, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

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