-1

I get this error:

Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int'

and it points at nnid.

How can I fix this?

// databaseden haberleri cek

-(NSMutableArray *)getNews: (NSInteger)nnid
{
    [database open];
    FMResultSet *results = [database executeQuery:
        @"SELECT * FROM news WHERE newsubid = ? order by ndate desc limit 0,50",
        [NSNumber numberWithInt:nnid]];

    NSMutableArray *tempdata2 = [NSMutableArray new];
    while ([results next]) {
        GundemNews* newsData = [GundemNews newsWithDBResultSet:results];
        [tempdata2 addObject:newsData];
    }
kryptics
  • 13
  • 5

2 Answers2

0

The warning message tells you the problem: you have an NSInteger, and you're trying to pass it as an int. Don't do that.

Why not? Well, on your platform, NSInteger is a typedef for long—hence the warning that says 'NSInteger' (aka 'long'). And a long is a 64-bit integer, which an int is 32 bits. So, an NSInteger can hold the number 4294967297, but when you pass it as an int, it will end us as 1. Or, worse, 3221225473 will end up as -1073741823.

On a different platform, NSInteger might be a typedef for int. But, while that would mean you get away with this code, it still wouldn't be good code—it would be code that mysteriously breaks as soon as you compile it on another platform.

Just use -numberWithInteger: instead of -numberWithInt:, like this:

FMResultSet *results = [database executeQuery:
    @"SELECT * FROM news WHERE newsubid = ? order by ndate desc limit 0,50",
    [NSNumber numberWithInteger:nnid]];

As the docs say, -numberWithInteger: "[c]reates and returns an NSNumber object containing a given value, treating it as an NSInteger."

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • NSInteger can be a long integer if it is running in a 64 bit environment... but it most certainly isn't just another name for long. You can very clearly see this in it's declaration: #if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64 typedef long NSInteger; typedef unsigned long NSUInteger; #else typedef int NSInteger; typedef unsigned int NSUInteger; #endif – TheCodingArt May 11 '15 at 02:39
  • @TheCodingArt: Sure it is. Otherwise, the compiler wouldn't give you a warning that says `'NSInteger' (aka 'long')`. It's either a `typedef` or a `#define` for `long` on every platform Cocoa runs on, except some platforms where `long` and `int` are the same size, in which case it doesn't matter. – abarnert May 11 '15 at 02:40
  • I just posted the code above. No it most certainly isn't, just like CGFloat, NSInteger is a dynamic type that changes based on it's environment. You can easily see this in the header declaration. – TheCodingArt May 11 '15 at 02:41
  • @TheCodingArt: And knowing how to read the compiler warnings is an important part of explaining the problem and solution to the OP, rather than just guessing or asking someone for code he doesn't understand. – abarnert May 11 '15 at 02:42
  • @TheCodingArt: The code you posted (by editing your comment after I'd written that response) very clearly shows that it's a `typedef` for `long`, just as I said it is. Except on some non-LP64 platforms, where `long` and `int` are the same size anyway, where it's a `typedef` for `int`. – abarnert May 11 '15 at 02:43
  • What? I'm telling you not to mis inform the OP which you are clearly doing. The compiler warning is misleading as it is confused as to what to do with the dynamic typecast which changes depending on it's environment. The same effect happens to CGFloat in Swift. This isn't worth a further discussion but shall be noted that NSInteger most definitely is not of type long int on all platforms. Just like CGFloat ranges from being a double and a float depending on platforms. – TheCodingArt May 11 '15 at 02:44
  • Yes, I'm just pointing out specifics if the answer is to define behavior it should define correct behavior. Especially if the OP doesn't know how a NSInteger works (in which they should look at the header files defining NSInteger). – TheCodingArt May 11 '15 at 02:47
0

You just need to change the line

FMResultSet *results = [database executeQuery:@"SELECT * FROM news WHERE newsubid = ? order by ndate desc limit 0,50", [NSNumber numberWithInt:nnid]];

to

FMResultSet *results = [database executeQuery:@"SELECT * FROM news WHERE newsubid = ? order by ndate desc limit 0,50", [NSNumber numberWithInteger:nnid]];

The warning occurs because you are trying to pass a NSInteger value to a method that expects a Int value.

FormigaNinja
  • 1,571
  • 1
  • 24
  • 36