Re: Strange NSString Behavior
Re: Strange NSString Behavior
- Subject: Re: Strange NSString Behavior
- From: Eric Gorr <email@hidden>
- Date: Thu, 26 Feb 2009 17:06:46 -0500
On Feb 26, 2009, at 12:58 AM, Kenneth Ramey wrote:
I have a method, written in Objective-C, to parse a string,
extracting several fields from it. Originally, I was pulling out a
couple of float values and some int fields. Other fields in the
string need to be treated as substrings. As I researched how to
deal with the substrings, I came to the conclusion that it might be
easier to treat all of the fields as substrings. So I re-wrote the
method to use NSString variables for each of the fields extracted
from the main string by an instance of NSScanner.
Now, when I run my program, I get the following error on the console
when execution enters the method in question:
Did you forget to nest alloc and init?
This message is repeated different numbers of time each execution.
By commenting out various parts of the code and using the debugger
to examine variables I find that one or more of the NSString
variables are actually listed as NSPlaceholderString variables and
there is a direct correlation between the number of
"...Placeholder..." variables and the number of messages that are
printed. If I comment out all the NSString variables, the messages
go away. Which variables appear to be "...Placeholder..." variables
also changes on each execution.
Here is the fragment of code in question. The basic template came
from an Apple example of using NSScanner:
- (void) processFixRecord:(NSString *) dataString
{
NSString *fixTime;
NSString *fixLat;
NSString *fixLong;
NSString *northSouth;
NSString *eastWest;
NSCharacterSet *skipSet = [NSCharacterSet
characterSetWithCharactersInString:@" ,\n"];
NSScanner *dataRecord = [[NSScanner alloc]
initWithString:dataString];
[dataRecord scanUpToCharactersFromSet:skipSet intoString:&fixTime];
...
Anyone have any suggestions? I also tried doing "alloc" and "init"
on each NSString (despite the Apple NSScanner example) with no
change in behavior and setting each variable to "nil", which also
produced no change.
I tried this myself with a simple test app which you can find at:
http://ericgorr.net/cocoadev/Scanner.zip
Basically, I just did:
@implementation AppController
- (void) awakeFromNib
{
NSString *fixTime;
NSString *dataString = @"hello there";
NSCharacterSet *skipSet = [NSCharacterSet
characterSetWithCharactersInString:@" ,\n"];
NSScanner *dataRecord = [[NSScanner alloc]
initWithString:dataString];
[dataRecord scanUpToCharactersFromSet:skipSet intoString:&fixTime];
NSLog( @"%@", fixTime );
}
@end
the output in the log after running the application:
******
******
******
[Session started at 2009-02-26 17:04:15 -0500.]
Loading program into debugger…
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40
UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i386-apple-darwin".tty /dev/ttys004
Program loaded.
sharedlibrary apply-load-rules all
run
[Switching to process 4115 local thread 0x2d03]
Running…
2009-02-26 17:04:18.479 Scanner[4115:813] hello
Debugger stopped.
Program exited with status value:0.
^^^^^^
^^^^^^
^^^^^^
It worked for me without any problems at all.
Unfortunately, I do not know what else you code might be doing that
would cause the behavior you are seeing.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden