简体   繁体   中英

String comparison in Objective-C with isEqualToString

I have a line of code that is cause a "EXC_BAD_ACCESS" error. The line of code is as follows (formatted into one line and nested code removed for ease of reading).

if (![sendData isEqualToString:@"-"]){ ... }

The actual error occurs on the IF line. the odd thing is that if I put a breakpoint on that line, the NSString called sendData (shown as NSCFString with a value of "-" without the quotes). Why would this be causing an error?

To catch this problem you'll have to put break points in all callback methods.

The problem is simple, the code is trying to access memory it cant find.

Finding that line of code is harder because the callbacks are not called sequentially.

  1. Add more break points
  2. Add more NSLog(..)
  3. Consider catching exceptions (see throwing an exception in objective-c/cocoa )

Output form the console:

Attaching to program: `/Users/rjstelling/Library/Application Support/iPhone Simulator/User/Applications/C04A40BB-1D98-402E-BBEF-37E6FB860089/TwoViewApp.app/TwoViewApp', process 24032.
Re-enabling shared library breakpoint 1
2009-04-16 16:16:45.830 TwoViewApp[24032:20b] stream event 1
2009-04-16 16:16:45.831 TwoViewApp[24032:20b] on input stream
2009-04-16 16:16:45.831 TwoViewApp[24032:20b] stream event 1
2009-04-16 16:16:45.832 TwoViewApp[24032:20b] on output stream
2009-04-16 16:16:45.832 TwoViewApp[24032:20b] stream event 4
2009-04-16 16:16:45.832 TwoViewApp[24032:20b] on output stream
2009-04-16 16:16:45.833 TwoViewApp[24032:20b] stream has space open
(gdb) continue
2009-04-16 16:17:06.405 TwoViewApp[24032:20b] We made it - ok!
2009-04-16 16:17:06.406 TwoViewApp[24032:20b] stream event 2
2009-04-16 16:17:06.406 TwoViewApp[24032:20b] on input stream
2009-04-16 16:17:06.407 TwoViewApp[24032:20b] Processing: +OK CONN PinkNotes® Plus Master v5.00.26 Beta (v4 compatible)
:tPNPStr
2009-04-16 16:17:06.407 TwoViewApp[24032:20b] SendData= USER (null):tPNPStr
Current language:  auto; currently objective-c
Program received signal:  “EXC_BAD_ACCESS”.
(gdb) 

The problem occurs some where in or after ProcessData which is a callback I think. Try and put a break point around line 157 in TwoViewAppAppDelegate.m


It's not that line that is causing the EXC_BAD_ACCESS if you add:

else
{
    NSLog(@"We made it - ok!");
}

To the if() statement you can see it passes over the if ( ![sendData isEqualToString:@"-"] ){...}

The error occurs when you return form the method call.


Ok form you comments this might help:

If you create strings using @"My string" the compiler will map these to he same memory if they have the same content, ie:

NSString *var1 = @"string1";
NSString *anotherstring = @"string1";
NSString *morestringivars = @"string1";

Will all point at the same memory space.

This may help, but I'm not sure how? Maybe you can post more code so I can run it on my set up.


Remember an auto release pool is created at the start of the event cycle on the iPhone.

Therefore it is a good idea to call autorelease on sendData as soon as you assign it to the ivar.

...

[sendData autorelease];

...

It sounds like you're trying to send a message to an object whose memory has been deallocated. Make sure you're following the proper memory management techniques discussed in the Memory Management Programming Guide for Cocoa . The fix would probably be to add a [sendData retain] at some point before it gets deallocated, but to understand why that works, you have to read the aforementioned guide.

在发布后将您的对象设置为 nil 以防止在向不可用对象发送消息时崩溃。

Man the problem is in this line

if([appVersion isNotEqualTo:currentVersion])

"isNotEqualTo" is supported at mac os x 10.X or later but not on iphone OS . so calling this causes an exception at device . now i hope you get the whole point instead change your comparison logic to if(![appVersion isEqualToString:currentVersion]) , i think it will work fine ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM