i get EXC_BAD_ACCESS while formatting string.
NSString *object = [[NSUserDefaults standardUserDefaults] stringForKey:@"ObjectNumber"];
NSString *pin = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithFormat:@"Pin%i ",object.intValue]];
NSString *msg = [NSString stringWithFormat:@"%@N", pin];
NSString *msg2 = @"0001N";
NSLog(@"Sending %@", msg);
tcpConnection *Obj = [tcpConnection alloc];
[Obj loadUpConnection:msg];
If i use msg2 everything works fine. But if i use msg it gets exc_bad_access even though NSLog prints msg correctly.
I suspect that your
loadUpConnection:method isn’t retaining its parameters.It seems that there’s a lot for you to learn in regards to objective-c.
I worry that this will confuse you more than help, but the
msg2variable is pointing to a static instance of the string"0001N"(because you hardcoded it at compile time the system creates a static instance to use). This is why it doesn’t crash when you usemsg2, but does when you usemsg.msgis pointing to a dynamically allocated instance. The reference returned to you is ‘autoreleased’ which means it will be released at some point in the future (usually at the end of the run loop iteration). If yourloadUpConnection:method doesn’t retain it’s parameters, then themsgstring will be released before you try to use it, causing theEXC_BAD_ACCESSerror. Becausemsg2is a static instance, it will never be deallocated, hence it not crashing.My only advice would be to continue learning – pick up a book, I recommend ‘Programming in Objecive-C’ by Stephen Kochan, or ‘iPhone Programming A Big Nerd Ranch Guide’ by Aaron Hillegass.