Bad access on NSString length - objective-c

with
NSString *responseData = [request responseString];
I get a string like (10).
Now I want to remove the brackets to get just the number. So I first wanted to get the length of the string
NSLog(#"%#",[responseData length]);
But I get a Thread 1: Program received signal: "EXC_BAD:ACCESS"
Any ideas?

NSString::length returns an unsigned integer. So, try -
NSLog(#"%i",[responseData length]); // format specifier is %i and not %#
// Even better if %u is used.
NSLog format specifiers

Related

How do you convert an entity's attribute of type int16 to string to be output to the user?

I have tried different methods for converting a value of type integer 16 to a string but none work. When an entity returns a numerical value, is it of a different type than what it is?
NSString *stringToGoOut =[#[entity valueForKey:#"value1"] stringValue];
NSString *stringToGoOut2 = [NSString stringWithFormat:#"%i", [entity valueForKey:#"value2"]];
the second one shows an error saying that the formate specifies type 'int' but the argument has type 'id_Nullable and request that i change the place holder from %i to %# for string but when i do that, it outputs 0 overtime regardless of what has been entered on the previous screen
NSString *stringToGoOut2 = [NSString stringWithFormat:#"%ld", (long)[entity valueForKey:#"value2"]];
this worked sort of, but the output was a long number (i am guessing the location in memory) to the user.
valueForKey: returns an object so to convert to a string either
NSString *stringToGoOut = [NSString stringWithFormat:#"%#", [entity valueForKey:#"value1"]];
or
NSString *stringToGoOut = [[entity valueForKey:#"value1"] stringValue];
will work.
If it is outputting 0 then [entity valueForKey:#"value1"] is probably an NSNumber
Core data stores all the numbers as NSNumber.
So if you would like to print out the value this line should work
NSString *stringToGoOut = [NSString stringWithFormat:#"%#", entity.value1];

Concatenating NSStrings with ints - ARC Error

I'm struggling with concatenating NSStrings and ints. In isolation the below code works great. It returns "This is a test string with an int 10"
int myInt =10;
NSString *newstring =
[NSString stringWithFormat: #"This is a test string with an int %i", myInt];
NSLog(#"%#", newstring);
However when I put the below code into my project i get an error: Implicit conversion of int to NSString is disallowed with arc."
[_mycrop setTempLeft: (#"left value %i is %i", count, [_mycrop leftValue])];
Could anybody suggest where I'm going wrong? Although im passing in 2 variables, to my mind both are essentially the same.
The code "in isolation" is very different from the second code.
You have to use stringWithFormat: in the second example too.
[_mycrop setTempLeft:[NSString stringWithFormat:#"left value %i is %i", count, [_mycrop leftValue]]];
Or with two lines but easier to understand:
NSString *tempLeft = [NSString stringWithFormat:#"left value %i is %i", count, [_mycrop leftValue]];
[_mycrop setTempLeft:tempLeft];
Some documentation: Apple String Programming Guide - Formatting String Objects

About the formation in NSLog in Objective-C

const char *string ="Hi there,this is a C string";
NSData *data=[NSData dataWithBytes:string
length:strlen(string)+1];
NSLog(#"data is %#",data);
NSLog(#"%lu byte string is '%s'",[data length],[data bytes]);
This can be implied successfully. If the last sentence is:
NSLog(#"%d byte string is '%s'",[data length],[data bytes]);
it will warn that conversion specifies type 'int' but argument has typed 'NSUInteger' (aka'usigned long')
Why %d can't?
NSUInteger is basically an unsigned long, so use %lu instead.
%d means 'int'. NSUInteger is not an 'int', so %d won't work. You have to match format specifiers with the type. If you specify the wrong type, your program can crash or more likely, it'll print garbage.

Cannot implicitly convert type 'integer' to 'NSString' -

I want to ask very simple thing about conversion between type.
int theinteger = 75;
NSLog(#"Before theinteger is: %#", theinteger);
NSString *string = [NSString stringWithFormat:#"%d", theinteger];
NSLog(#"After theinteger is: %#", string);
Output is like
Before theinteger: 75
After theinteger: 114503696
What does this mean? Why after conversion my data is changed to 114503696?
When I tried
int theinteger = -1;
It's OK
Before theinteger: -1
After theinteger: -1
Am I missing something?
Without seeing any evidence of it, I assume theinteger is, in fact, of type NSInteger or int.
When using a specifier, try using %i:
NSInteger theinteger = 75;
NSLog(#"theinteger is: %i", theinteger);
Output
theinteger is: 75
Here's a list of specifiers.
You shouldn't be using %# as a format specifier for an integer, as in your first log statement. However, my guess is the code you've posted here isn't actually the code you're using, because this line NSLog(#"Before theinteger is: %#", the integer); won't actually compile due to the space in "the integer". Can you copy/paste your actual code?
Anyway, %# is the format specifier for Objective-C objects. When NSLog() sees a %#, it substitutes it with the NSString returned by calling -(NSString *)description on the corresponding object in the variables list. In your case, NSLog() sees the %# and assumes that means that theinteger is an Objective-C object, which it is not.
If you want to print an integer, you should use a format specifier of %i (or another of the several integer format specifiers):
NSLog(#"Before theinteger is: %i", theinteger);

NSLog incorrect encoding

I've got a problem with the following code:
NSString *strValue=#"你好";
char temp[200];
strcpy(temp, [strValue UTF8String]);
printf("%s", temp);
NSLog(#"%s", temp);
in the first line of the codes, two Chinese characters are double quoted. The problem is printf function can display the Chinese characters properly, but NSLog can't.
Thanks to all. I figured out a solution for this problem. Foundation uses UTF-16 by default, so in order to use NSLog to output the c string in the example, I have to use cStringUsingEncoding to get UTF-16 c string and use %S to replace %s.
NSString *strValue=#"你好";
char temp[200];
strcpy(temp, [strValue UTF8String]);
printf("%s", temp);
strcpy(temp, [strValue cStringUsingEncoding:NSUTF16LittleEndianStringEncoding]);
NSLog(#"%S", temp);
NSLog's %s format specifier is in the system encoding, which seems to always be MacRoman and not unicode, so it can only display characters in MacRoman encoding. Your best option with NSLog is just to use the native object format specifier %# and pass the NSString directly instead of converting it to a C String. If you only have a C string and you want to use NSLog to display a message instead of printf or asl, you will have to do something like Don suggests in order to convert the string to an NSString object first.
So, all of these should display the expected string:
NSString *str = #"你好";
const char *cstr = [str UTF8String];
NSLog(#"%#", str);
printf("%s\n", cstr);
NSLog(#"%#", [NSString stringWithUTF8String:cstr]);
If you do decide to use asl, note that while it accepts strings in UTF8 format and passes the correct encoding to the syslog daemon (so it will show up properly in the console), it encodes the string for visual encoding when displaying to the terminal or logging to a file handle, so non-ASCII values will be displayed as escaped character sequences.
My guess is that NSLog assumes a different encoding for 8-bit C-strings than UTF-8, and it may be one that doesn't support Chinese characters. Awkward as it is, you might try this:
NSLog(#"%#", [NSString stringWithCString: temp encoding: NSUTF8StringEncoding]);
I know you are probably looking for an answer that will help you understand what's going on.
But this is what you could do to solve your problem right now:
NSLog(#"%#", strValue);
# define NSLogUTF8(a,b) NSLog(a,[NSString stringWithCString:[[NSString stringWithFormat:#"%#",b] cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding])
#define NSLogUTF8Ex(a,b) NSLog(a,[MLTool utf8toNString:[NSString stringWithFormat:#"%#",b]])
+(NSString*)utf8toNString:(NSString*)str{
NSString* strT= [str stringByReplacingOccurrencesOfString:#"\\U" withString:#"\\u"];
//NSString *strT = [strTemp mutableCopy];
CFStringRef transform = CFSTR("Any-Hex/Java");
CFStringTransform((__bridge CFMutableStringRef)strT, NULL, transform, YES);
return strT;
}