20

Using MagicalRecord, I am trying to get the record with a particular clientNumber which is a NSInteger (defined as int16 as the data type).

This is my line of code where I'm getting the error:

ClientInfo *clientSelected = [ClientInfo MR_findFirstByAttribute:@"aClientNumber" withValue: clientNumber inContext:localContext];

UPDATE: This is the definition of MR_findFirstByAtytribute:

MR_findFirstByAttribute:(NSString *) withValue:(id)

This is the error I'm getting:

Implicit conversion of NSInteger (aka int) is disallowed with ARC

For the life of me, I don't see what's wrong. ClientInfo is defined as

@interface ClientInfo : NSManagedObject
SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • It's saying you're trying to use NSInteger -- a "scalar" -- where an object pointer is expected. – Hot Licks Dec 19 '12 at 17:15
  • That makes absolutely no sense to me... then what's it supposed to be? – SpokaneDude Dec 19 '12 at 17:19
  • We can't tell precisely what it's supposed to be without seeing the MR_findFirstAttribute method description, but it it's expecting an NSNumber you need to use `[NSNumber numberWithInt:clientNumber]` – Hot Licks Dec 19 '12 at 17:22
  • (I agree that it's a stupid -- very unhelpful -- error message. Even those of us with decades in the biz were scratching our heads for a couple of minutes the first time we saw it.) – Hot Licks Dec 19 '12 at 17:24
  • Updated the question... still not working with [NSNumber numberWithInt: clientNumber]... clientNumber is defined as NSInteger. The problem appears to be the conversion of NSInteger to id, which I can't seem to figure out. – SpokaneDude Dec 19 '12 at 17:27
  • You should not still be getting the "Implicit conversion" message if you're passing an NSNumber as the second parameter on that call. (But note that you did not include the entire definition of that method -- you're missing the inContext parm.) – Hot Licks Dec 19 '12 at 17:30
  • (Though you might get a different "Implicit conversion" message from numberWithInt, if passing an NSInteger. Try numberWithInteger.) – Hot Licks Dec 19 '12 at 17:33
  • You could also use the new literals syntax: `ClientInfo *clientSelected = [ClientInfo MR_findFirstByAttribute:@"aClientNumber" withValue: @(clientNumber) inContext:localContext];` – Alladinian Dec 19 '12 at 17:34
  • HotLicks: please take your comment and make an Answer with it (the one about using [NSNumber numberWithInteger:clientNumber])... it got rid of the error, and I now understand what to do. Thank you. – SpokaneDude Dec 19 '12 at 17:36
  • You can check the answer by @mipadi -- it's essentially the same and he posted about the same time. – Hot Licks Dec 19 '12 at 23:44

2 Answers2

36

The parameter type for withValue is an id (a pointer). NSInteger is a scalar value (not an object) and cannot be converted to a pointer value implicitly.

This is purely a guess, but creating an NSNumber from the NSInteger might work:

NSNumber *val = [NSNumber numberWithInteger:clientNumber];
ClientInfo *clientSelected = [ClientInfo MR_findFirstByAttribute:@"aClientNumber" withValue:val inContext:localContext];
Maetschl
  • 1,330
  • 14
  • 23
mipadi
  • 398,885
  • 90
  • 523
  • 479
-1

As per your definition of method it's expecting and id and your are passing it a NSInteger aka int

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184