Re: newbie EXC_BAD_ACCESS
Re: newbie EXC_BAD_ACCESS
- Subject: Re: newbie EXC_BAD_ACCESS
- From: mmalcolm crawford <email@hidden>
- Date: Sun, 20 Mar 2005 18:33:15 -0800
On Mar 20, 2005, at 5:54 PM, Larry Fransson wrote:
[scanner scanUpToCharactersFromSet: chSetTab intoString:
&scannedName];
This line is going to cause a problem. You have declared
scannedName, but it hasn't been allocated or initialized, so it's
pointing nowhere. That will cause a crash.
This would be a good place for an autoreleased object. When you
declare scannedName, initialize it using [NSString string]. Then
you don't need to worry about cleaning it up afterward.
Umm, no -- the argument is a pointer to a pointer...
<http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
ObjC_classic/Classes/NSScanner.html#//apple_ref/doc/uid/20000159/
scanUpToCharactersFromSet_intoString_>
"- (BOOL)scanUpToCharactersFromSet:(NSCharacterSet *)stopSet
intoString:(NSString **)stringValue
Scans the string until a character from stopSet is encountered,
accumulating characters into a string that’s returned by reference in
stringValue."
mmalc
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSCharacterSet *chSetRtn = [NSCharacterSet
characterSetWithCharactersInString:@"\n"];
NSString *string = @"aaa\nbbb\nccc\nddd\neee";
NSScanner *scanner = [NSScanner scannerWithString:string];
NSString *scannedName;
while(![scanner isAtEnd]) {
if([scanner scanUpToCharactersFromSet:chSetRtn
intoString:&scannedName]) {
NSLog(@"scanned %@", scannedName);
}
}
[pool release];
return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden