-3

I wrote code.

I want to save integer using nsuserdefault.

But this code happen EXC_BAD_ACCESS and SIGABRT.


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    [self desave:[newys integerValue] :[newms integerValue]];
    ...
}

...

- (void)desave:(NSInteger*)year :(NSInteger*)month//←EXC BAD ACCESS happened on this row.
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setInteger:year forKey:@"year"];//←SIGABRT happened on this row.

    [defaults synchronize];
}
Rukkora
  • 186
  • 2
  • 11
  • Post the stacktrace (`year` will almost certainly be `nil`, which is the problem). – trojanfoe Aug 20 '13 at 15:20
  • 9
    So you chose to ignore the "incompatible pointer to integer conversion" compiler warnings? – Martin R Aug 20 '13 at 15:24
  • I think there is some confusion between `NSNumber` and `NSInteger`; http://stackoverflow.com/q/5870867/620197 – Mike D Aug 20 '13 at 15:29
  • @martin +1 - symbolically since I accidentally undid my up vote and cannot up vote again as it seems :) – Mario Aug 20 '13 at 15:33
  • 2
    Not related, but please use standard naming conventions and name your parameters. `(void)desaveYear:(NSInteger*)year month:(NSInteger*)month` is definitely less awkward to read and use. – Gabriele Petronella Aug 20 '13 at 15:47
  • This seems like a pretty legit question over a misunderstanding of pointers. – Ben Flynn Aug 21 '13 at 01:16

4 Answers4

2

Based on your desave method signature, you're passing an NSInteger* as an argument where an NSInteger is expected. NSInteger is not an object derived from NSObject, it's a primitive.

Paul Dardeau
  • 2,589
  • 1
  • 13
  • 10
2

The problem is caused by the needless pointers being used for the NSInteger parameters.

Change:

- (void)desave:(NSInteger*)year :(NSInteger*)month

to:

- (void)desave:(NSInteger)year :(NSInteger)month

NSInteger is not a class type.

You should also name your method a little better. Right now the name is desave::. It would be better if it was:

- (void)desaveYear:(NSInteger*)year month:(NSInteger*)month

Now the name is desaveYear:month:.

And now you can call it with:

[self desaveYear:[newys integerValue] month:[newms integerValue]];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

You are passing a pointer to an NSInteger. The simplest way to solve this is:

- (void)desave:(NSInteger)year :(NSInteger)month
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:year forKey:@"year"];
    [defaults synchronize];
}
Mike D
  • 4,938
  • 6
  • 43
  • 99
0

On your method

- (void)desave:(NSInteger*)year :(NSInteger*)month

You are sending both year and month variables pointers. This sends the memory addresses instead. Try with this instead

- (void)desave:(NSInteger)year :(NSInteger)month
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:year forKey:@"year"];
    [defaults synchronize];
}
CoderPug
  • 908
  • 9
  • 19