Domanda

since xCode 5.1 I get to following warning:

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

at the following line:

(void) CFStreamCreatePairWithSocketToCFHost(NULL, host, port, &readStream,&writeStream);

marked ist 'port'

this is the declaration:

+ (void)getStreamsToHostNamed:(NSString *)hostName port:(NSInteger)port inputStream:(NSInputStream **)inputStream outputStream:(NSOutputStream **)outputStream

Can anybody help me with this warning?

È stato utile?

Soluzione

Since the third parameter of CFStreamCreatePairWithSocketToCFHost is of type SInt32, all you need is to change the signature of your getStreamsToHostNamed method as follows:

+ (void)getStreamsToHostNamed:(NSString *)hostName
                         port:(SInt32)port
                  inputStream:(NSInputStream **)inputStream
                 outputStream:(NSOutputStream **)outputStream

In general, you should avoid implicit conversions between NS wrappers of integer types, because these have platform-dependent implementations. Fortunately, compiler issues warnings about this, so you don't have to track them down manually.

Altri suggerimenti

The best solution I see is to change the NSInteger to SInt32 or change the SInt32 to NSInteger, depending on how big the numbers need to be. If you are only going to be using between -2147483648 and 2147483647, then SInt32 should be good enough. If that is the case and there is no way of changing either one, just use an explicit cast to SInt32 to bypass the warning, though it could affect the results if the number is out of that range.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top