NSString outofscope issue - iphone

My code is:
- (void)viewDidLoad {
[super viewDidLoad];
self.appDelegate=[[UIApplication sharedApplication]delegate];
self.dateString=[NSString stringWithFormat:#"%#",appDelegate.tappedDate];
dateLabel.text=dateString;
}
-(IBAction)checkForData:(id)sender{
NSString *bday=#"2012-01-26";
if(bday==dateString)
{
UIAlertView *bdayView=[[UIAlertView alloc]initWithTitle:#"Birthday!!!" message:#"Its ur Best Friend's Bday" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[bdayView show];
[bdayView release];
}
else{
UIAlertView *bdayView=[[UIAlertView alloc]initWithTitle:#"No Data" message:#"No Data available for this date" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[bdayView show];
[bdayView release];
}
}
The String dateString is going out of scope in the if condition but it is displaying data on the label.

First of all, bday will never equal dateString, as == compares the addresses of both objects. If you want to compare the actual strings, you need to do if ([bday isEqualToString:dateString]) {...}
Regarding the out-of-scope message: How does the property for dateString look like? You need to provide more details. Commonly, it should look like #property (nonatomic, copy) NSString *dateString

Related

EXC_BAD_ACCESS error - sometimes only

my app has a button that checks whether an entered value is correct.
sometimes it causes it to crash, but the strange thing is that it happens at irregular intervals (sometimes on the third iteration, sometimes the tenth, sometimes never).
i get a EXC_BAD_ACCESS error in the debugger. so it seems like something is being released when it shouldn't be. the button calls this function:
- (IBAction)checkValue:(id)sender{
int actualDifference = [firstNumberString intValue] - [secondNumberString intValue];
actualDifferenceAsString = [NSString stringWithFormat:#"%d", actualDifference];
if ([answerTextField.text isEqualToString:actualDifferenceAsString])
{
UIAlertView *correctAlert = [[UIAlertView alloc] initWithTitle:#"matches"
message:#"next value."
delegate:nil
cancelButtonTitle:#"ok"
otherButtonTitles: nil];
[correctAlert show];
[correctAlert release];
}
else
{
UIAlertView *incorrectAlert = [[UIAlertView alloc]
initWithTitle:#"does not match"
message:#"next value."
delegate:nil
cancelButtonTitle:#"ok"
otherButtonTitles: nil];
[incorrectAlert show];
[incorrectAlert release];
}
using zombies pointed to the first statement:
int actualDifference = [firstNumberString intValue] - [secondNumberString intValue];
does anyone know what the problem might be?
If zombies are detected in the first line, that means some other part of your program is releasing firstNumberString or secondNumberString. That's where the problem starts, but it only shows up here, when you later try to access those values. Where else do you work with those strings? Do you ever release them?
For overall safety, they should probably be assigned properties, not member variables.
Change it into
NSInteger actualDifference = [firstNumberString intValue] - [secondNumberString intValue]; //change int to NSInteger
NSString *actualDifferenceAsString = [NSString stringWithFormat:#"%d", actualDifference];
if ([answerTextField.text isEqualToString:actualDifferenceAsString])
{
UIAlertView *correctAlert = [[UIAlertView alloc] initWithTitle:#"matches"
message:#"next value."
delegate:nil
cancelButtonTitle:#"ok"
otherButtonTitles: nil];
[correctAlert show];
[correctAlert release];
}
else
{
UIAlertView *incorrectAlert = [[UIAlertView alloc]
initWithTitle:#"does not match"
message:#"next value."
delegate:nil
cancelButtonTitle:#"ok"
otherButtonTitles: nil];
[incorrectAlert show];
[incorrectAlert release];
}
you get this code. It worked for me...

[__NSCFSet iName]: unrecognized selector sent to instance

I just do not seem to follow this.
This is my code
-(void)saveClicked:(id)sender{
Item *item=[[Item alloc]init];
item.iName=nameField.text;
if ([appDelegate.list containsObject:item]) {
//currentItem and item are object of class Item
//currentItem was declared in the headerfile and synchronized
currentItem=item;
NSString *msg=[NSString stringWithFormat:#"%# already exists in your Instock list",item.iName];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"edit",nil];
[alert show];
[alert release];
}
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex==0) {
}
else {
//getting the error here
NSLog(#"%#",currentItem.iName);
}
}
ERROR:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet iName]: unrecognized selector sent to instance 0x5e280b0'
I have no clue why this is happening. Help would be appreciated.
Assume you have currentItem as retain property , Try with using below with your saveClicked:
self.currentItem = item;
So, you code should be ...
-(void)saveClicked:(id)sender{
Item *item=[[Item alloc]init];
item.iName=nameField.text;
if ([appDelegate.list containsObject:item]) {
//currentItem and item are object of class Item
//currentItem was declared in the headerfile and synchronized
self.currentItem = item;
NSString *msg=[NSString stringWithFormat:#"%# already exists in your Instock list",item.iName];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"edit",nil];
[alert show];
[alert release];
}
[item release];
}
It doesn't make sense to release item as that would mean you've taken ownership only once and you're relinquishing it. So the currentItem will be pointing to a deallocated object. You will have to take ownership by retaining the object and later releasing it once you're done with the object.

Expected Expression before ":" token

I am getting this error in first two lines of code given below
and the error is "Expected Expression before ":" token"....
some one please help me to overwrite this....
(IBAction)buttonPressed {
NSInteger stateRow =[picker selectedRowInComponent:kStateComponent];
NSInteger zipRow = [picker selectedRowInComponent:kZipComponent];
NSString *state=[self.states objectAtIndex:stateRow];
NSString *zip=[self.zips objectAtIndex:zipRow]
NSString *title=[[NSString alloc]initWithFormat:#"You selected zip code %#",zip];
NSString *message=[[NSString alloc]initWithFormat:#"%# is in %#",zip,state];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:title message:message delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[title release];
[message release];
}
You have to write it as
(IBAction)buttonPressed {
Infact it should be
-(IBAction)buttonPressed {
May be - is missing that might be creating this error.
Hope this helps.
EDIT:
If still it doesnt help, then you may try this.
-(IBAction)buttonPressed:(id)sender {
EDIT-2:
Also you are missing a ; at the end of statement no 4
that is
NSString *zip=[self.zips objectAtIndex:zipRow]
You should add a semicolon(;) there at the end
Hope this solves it
There's a - missing right at the beginning before (IBACTION).
I have tested this code and its working properly.
change the "(NSInteger)component" as per requirement of picker.
- (IBAction) buttonPressed:(id)sender {
NSInteger stateRow =[pickerView selectedRowInComponent:"(NSInteger)component"];
NSInteger zipRow = [pickerView selectedRowInComponent:"(NSInteger)component"];
NSLog(#"stateRow - %d",stateRow);
NSLog(#"zipRow - %d",zipRow);
}
And also put - (IBAction) buttonPressed:(id)sender; in .h file

iPhone: Using Alerts to Help Debugging

I've been building a rather complex system and there's come the time now where I want more concise debugging. I would like to display the contents of a variable (for this example an NSString called v_string) in a notification window (the kind of window that appear when you receive an SMS text).
Is there an easy way to just call an alert with a variable?
Thanks in Advance,
Dan
NSLog does not do? If not (like if you need to debug an application running on a disconnected device), you can extend the UIAlertView with a category:
#implementation UIAlertView (Logging)
+ (void) log: (id <NSObject>) anObject
{
NSString *message = [anObject description];
UIAlertView *alert = [[self alloc] initWith…];
[alert show];
[alert release];
}
And then in code:
NSString *anInterestingString = …;
[UIAlertView log:anInterestingString];
When you build the string to display in the alert window, simply append your variable's string represenation using stringByAppendingString.
Alert window is cumbersome. Use NSLog instead:
NSLog(#"Variable is: %#", v_string);
And in Xcode's console you will see that text.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"My Debug String" message:v_string delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[message show];
[message release];
I think this way you can see what you want.
But, as zoul said, why not to use NSLog(#"my var: %#", v_string); ?
Hope that it helps.

Am I releasing memory correctly?

I have the following helper object:
LikeHelper* likeHelper = [[LikeHelper alloc]init];
likeHelper.delegate = self;
[likeHelper performLike:self.messageID];
[likeHelper release];likeHelper=nil;
performLike will do some NSURLConnection stuff and then tell the delegate whether or not it was successful.
#pragma mark LikeHelperDelegate Methods
-(void)performLikeFinished:(BOOL)isSuccessful{
if (isSuccessful) {
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"The message has been liked" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
else {
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:#"Error!" message:#"There was a problem liking your message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
Am I releasing the likeHelper in the right place?
If the performLike: method is asynchronous, the likeHelper will propably be released before the performLikeFinished: method is called. You should release the likeHelper in the dealloc: method of the owner object or in the performLikeFinished: implementation in the LikeHelperDelegate to prevent releasing it too soon but if you do that, be aware of JeremyPs comment below!.
If the performLike: method is synchronous, you are doing the right thing but you wouldn't need the delegate to collect the result.
Yes you are, your code is according to the guidelines.
If your code do not work then the problem might be that LikeHelper need to retain self from within -[LikeHelper performLike:].
You should also not retain the LikeHelperDelegate, that might be another cause of confusion or errors.