I am using xcode5 and ios7 and the compiler shows me this error:
Implicit conversion loses integer precision long long to NSInteger
if (statusCode == 200 && !upload) {
totalBytesExpectedToRead = [response expectedContentLength];
Any help?
NSURLResponse expectedContentLength returns type long long.
I suspect you have declared your totalBytesExpectedToRead variable as an NSInteger, if you make it long long the error will go away.
long long totalBytesExpectedToRead;
Related
I've been trying to call the function below.It seems that whenever in the function playNote I'm trying to access the object that I'm passing as the argument (myNum) it always crashes. I'm very new and I probably don't understand how to pass parameters through CCCallFuncND. All comments are appreciated.
This is the call that passes the argument myNum:
id action2 = [CCCallFuncND actionWithTarget:self selector:#selector(playNote:data:) data:(NSNumber *)myNum];
This is the whole block:
- (void)muteAndPlayNote:(NSInteger)noteValue :(CCLayer*)currentLayer
{
myNum = [NSNumber numberWithInteger:noteValue];
NSLog(#"Test the number: %d", [myNum integerValue]);
id action1 = [CCCallFunc actionWithTarget:self selector:#selector(muteAudioInput)];
id action2 = [CCCallFuncND actionWithTarget:self selector:#selector(playNote:data:) data:(NSNumber *)myNum];
id action3 = [CCDelayTime actionWithDuration:3];
id action4 = [CCCallFunc actionWithTarget:self selector:#selector(unmuteAudioInput)];
[currentLayer runAction: [CCSequence actions:action1, action2, action3, action4, nil]];
}
NSLog never displays anything it crashes at this line.
- (void) playNote:(id)sender data:(NSNumber *)MIDInoteValue
{
NSLog(#"Test number 2: %d", [MIDInoteValue integerValue]);
int myInt = [MIDInoteValue floatValue];
[PdBase sendFloat: 55 toReceiver:#"midinote"];
[PdBase sendBangToReceiver:#"trigger"];
}
Note that CCCallFunc* actions are inherently unsafe if you're using ARC.
Regardless of that it's generally better to use CCCallBlock* actions (which are safe to use under ARC) because then you often don't even need to pass in data as parameters, you can just use the variables of the local scope inside the block:
myNum = [NSNumber numberWithInteger:noteValue];
[CCCallBlock actionWithBlock:^{
NSInteger myInt = [myNum integerValue];
// do something with myInt, or just use noteValue directly
}];
PS: check your code for data type consistency. You create NSNumber myNum as an NSInteger value, later you get it via the floatValue method which implicitly converts the number to float and then back to int (use integerValue instead). You assign it to an int value which is the same as int only on 32 bit systems, on 64 bit systems like iPhone 5S NSInteger is actually a 64 bit type (use NSInteger instead of int).
You can get nasty value conversion issues (as well as issues when building for 64 bit devices) if you aren't consistent in using the exact same data type. Plus you're possibly even getting warnings about this already - take those warnings seriously.
Your method signature is:
-(void)playNote:(id)sender data:(NSNumber*)MIDInoteValue
but it should be:
-(void)playNote:(id)sender data:(void*)data
This is defined in the CCActionInstant.h as:
typedef void (*CC_CALLBACK_ND)(id, SEL, id, void *);
Also I'm pretty sure you get some information from the crash, like the call stack end the console output, would be helpful to paste it here in case I'm wrong ;)
For those encountering problems with this function, here are the working versions:
id action2 = [CCCallFuncND actionWithTarget:self selector:#selector(playNote:data:) data:(void *)noteValue];
and then the definition:
- (void) playNote:(id)sender data:(void *)midiNoteCode
{
int myNum = midiNoteCode; //void * to int conversion may cause problems on 64bit platform, wrap it into NSInteger
[PdBase sendFloat: (float)myNum toReceiver:#"midinote"];
[PdBase sendBangToReceiver:#"trigger"];
}
I am having issues with a category method used to percent-escape illegal symbols.
This is the code that i am using for the task:
#implementation NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
NSString *s = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",CFStringConvertNSStringEncodingToEncoding(encoding));
NSLog(#"S: %#, Self: %#", s, self);
return [s autorelease];
}
#end
When ever i run this method on a string without any of the symbols found in the above matching-string, the method runs fine and the same string is returned back to me.
For instance if i have a string like #"test" it will output:
S: test, Self: test
But if i instead use a string like #"test&symbols" it will output:
S: null, Self: test&symbols
Hence something seems to be wrong with the use of CFURLCreateStringByAddingPercentEscapes.
Now i want to escape symbols such as & because they can occur in strings used as values in a query string, which would cause the query string to be misinterpreted.
Any idea's about what may be the issue here?
Thank you in advance! / Magnus
After finding out my big mistake.
I was told to answer the question to my self.
What i did wrong that i didn't pass NSStringEncoding value to the method, like NSUTF8StringEncoding but instead of that I was passing a CF value such as kCFStringEncodingUTF8.
The value is passed thru a converter to make it CF value which caused an error and it was already had the correct type.
Sorry for any inconvenience.
-Magnus
I am getting a casting error. My app is reading a text file from a webpage using 'stringWithContentsOfURL' method. I want to parse the individual lines into separate components. This is a snippet of the code.
int parameterFive_1 = 0;
parameterFive_1_range = NSMakeRange(0,10)
lines = [response componentsSeparatedByString:#"\r"];
parameterFive_1 = CFStringGetIntValue([[lines objectAtIndex:i] substringWithRange:parameterFive_1_range]);
I am getting the following error message:
" Implicit conversion of an Objective-C pointer to 'CFStringRef' (aka 'const struct __CFString *') is disallowed with ARC"
I thought it might be the compiler option but changing it to the default is not making a difference. Can anyone provide any insight?
Just cast the NSString* to CFStringRef to satisfy ARC:
parameterFive_1 = CFStringGetIntValue((__bridge CFStringRef)[[lines objectAtIndex:i] substringWithRange:parameterFive_1_range]);
The __bridge keyword here lets ARC know that it doesn't need to transfer ownership of the string.
please see code as follow :
- (BOOL)postAction :( NSString*) url params:(NSDictionary*) params bodySize:(NSNumber**)bodySize
{
...
int size = 1999;
NSNumber* value =[[NSNumber alloc] initWithInt:size];
bodySize = &value;
...}
use the function as follows:
NSNumber* size ;
[self postAction:#"http://webserver/ss.php" params:params bodySize:&size];
// can not got the size value at all…
int i = [size intValue];
//will throw nil exception !
my question is that how to correct this code above ?
many thanks for your help !
Regards
If you are using pass-by-reference in iOS or Mac OS X, you are probably doing it wrong.
Seriously-- pass by reference is an exceedingly rare pattern to use. It is pretty much entirely reserved to (NSError**) across the APIs.
Not to say that there isn't a reason to use pass-by-reference, but this isn't it.
Specifically, if you need to return an NSNumber, then return it!
- (NSNumber *) foo;
If that method returns nil, that is just as good as returning a BOOL NO. And it sets you up to follow the very common pattern of using NSError:
- (NSNumber *) foo: (NSError **) error;
Ideally, you should always check the pointer before dereferencing it:
If ( bodySize )
*bodySize = value;
I think you want this:
//bodySize = &value;
*bodySize = value;
I agree with the above two answers. To be more clear, NSNumber is an object (Unlike NSInteger or NSUInteger). So you should directly point your pointer to that object.
I'm doing a check in an iPhone application -
int var;
if (var != nil)
It works, but in X-Code this is generating a warning "comparison between pointer and integer." How do I fix it?
I come from the Java world, where I'm pretty sure the above statement would fail on compliation.
Primitives can't be nil. nil is reserved for pointers to Objective-C objects. nil is technically a pointer type, and mixing pointers and integers will without a cast will almost always result in a compiler warning, with one exception: it's perfectly ok to implicitly convert the integer 0 to a pointer without a cast.
If you want to distinguish between 0 and "no value", use the NSNumber class:
NSNumber *num = [NSNumber numberWithInt:0];
if(num == nil) // compare against nil
; // do one thing
else if([num intValue] == 0) // compare against 0
; // do another thing
if (var) {
...
}
Welcome to the wonderful world of C. Any value not equal to the integer 0 or a null pointer is true.
But you have a bug: ints cannot be null. They're value types just like in Java.
If you want to "box" the integer, then you need to ask it for its address:
int can_never_be_null = 42; // int in Java
int *can_be_null = &can_never_be_null; // Integer in Java
*can_be_null = 0; // Integer.set or whatever
can_be_null = 0; // This is setting "the box" to null,
// NOT setting the integer value