Paul
Paul

Reputation: 193

Cocoa NSInteger when building in 64bit architecture

I have an NSSegmentedControl set up as a sender, adding functions when the user clicks through each button in the object. The snippet below always worked fine for me. It still does, however I'm getting the following warning when I build using 64bit architecture. The warning goes away when I revert to 32 bit. Can someone please advise me on how to edit the code? Thanks for the help. -paul.

"implicit conversion loses integer precision: NSInteger (aka long) to "int"

int selectedSegment = [arSegController selectedSegment];
int clickedSegmentTag = [[arSegController cell] tagForSegment:selectedSegment];

if (clickedSegmentTag == 0) {

Upvotes: 0

Views: 1163

Answers (4)

user529758
user529758

Reputation:

NSInteger seems to be defined to long on 64-bit platforms (so on 32-bit OS X and iOS you won't get this warning). Also, on these platforms, long is 64 bits long, while int is 32 bit only - so, as the compiler tells you, you loose precision. Numbers >= 2 ^ 32 cannot be represented in 32 bit, so for these numbers, you would likely experience some odd behavior (related to the numbers getting truncated).

Solution: explicitly cast the return value of the method to (int).

Upvotes: 0

Tom Fobear
Tom Fobear

Reputation: 6749

64 bit guide

I believe you are assigning a long variable of 8 bytes long to a an int that is 4 bytes long. I think in mac they use LP64 which means longs and pointers get 8 bytes. declaring selectedSegment NSInteger as well should remedy the problem. On 32 bit machines long would be 32 bits, while on 64 bit machines it would be 64 bits.

NSInteger selectedSegment = [arSegController selectedSegment]; 
NSInteger clickedSegmentTag = [[arSegController cell] tagForSegment:selectedSegment]; 

if (clickedSegmentTag == 0) { 

Upvotes: 1

sud0
sud0

Reputation: 547

An NSInteger is nothing more than a typedef'd long. You should be using NSInteger but if for some reason you NEED to use int then try typecasting.

int selectedSegment = (int)[arSegController selectedSegment];

Upvotes: 1

Nicholas Riley
Nicholas Riley

Reputation: 44361

You need to use NSInteger instead of int for your variables. You can just look at the declarations in NSSegmentedControl.h, but better is to read Apple's 64-Bit Transition Guide for Cocoa.

Upvotes: 1

Related Questions