[__NSCFSet iName]: unrecognized selector sent to instance - iphone

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.

Related

EXC_BAD_ACCESS code 2 on UIAlertView in iOS6

I'm trying to figure out why im getting this crash in my app.
It works perfectly fine in Xcode 4.4 running in the simulator with ios5.1, but when i switch into xcode 4.5 and ios6 I'm getting an EXC_BAD_ACCESS code 2. Here is my code:
- (void) myMethod
{
UIAlertView *alertview = [[[UIAlertView alloc]initWithTitle:#"Title" message:#"message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
alertview.tag = 1
[alertview show];
}
this is giving me an EXC_BAD_ACCESS code 2 on the [UIAlertView show] line
any ideas?
thanks!
I've got it.
I have the same problem, in my case it seems that the method is thrown from background now (now in ios7, in ios6 UIAlertView was automatically put into the main-thread as #nodepond says -thanks!-)..
try to assure that the method is shown from main thread:
[alertView performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
Good luck!
It's happened with me, even in 2014.
The problem is want to use an object already released.
What I did wrong:
//class B with UIAletViewDelegate
-(void) showAlert{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle bla bla...];
[alert show];
}
//class A
viewDidLoad{
MyClassB *B = [[B alloc] init];
[B showAlert];
}
What is the right way:
//Class A
#implementation A{
ClassB *B;
}
viewDidLoad{
B = [[B alloc] init];
[B showAlert];
}

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...

NSString outofscope issue

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

how to pass a Parameter from a Method which opens a UIAlertView to the UIAlertViewDelegate-Method?

Given the following code:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (mainViewController.loggedIn) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"test" message:#"notification received. view now?" delegate:self cancelButtonTitle:#"Later" otherButtonTitles: #"Show"), nil];
NSString *hash = [userInfo objectForKey:#"id"];
[alert setValue:hash forKey:#"hash"];
[alert show];
[alert release];
}
}
Here I try to set a value (hash) for the key #"hash". I'm doing this, because I need this hash value in case the user presses the "Show" button. Then - in the delegate method - I try to read the value again:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *hash = [alertView valueForKey:#"hash"];
if (hash != nil) {
// send hash to server to show the correct site
}
}
}
But as soon as i call[alert setValue:hash forKey:#"hash"]; my iphone app crashes.
This is what i see in the console:
* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key hash.'
Does anybody has an idea what I made wrong. Or how could i pass my "hash" value to the clickedButtonAtIndex method?
Declare hash as NSString class member. While you are showing the alertview, set the hash variable. Also, set a tag to the alertview.
In clickedButtonAtIndex method, check the alertview tag and use the hash value if required.

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.