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
Related
In Java, I would do this:
String temp = ".";
System.out.println(temp);
How might I do that in Objective-C?
Objective-C doesn't really have its own dedicated printing function. NSLog will print what you give it along with a bunch of debugging information. For straight-up printing, the C printf() is still basically the standard. As the equivalent to your code, I'd write:
NSString *temp = #".";
printf("%s\n", [temp UTF8String]);
NSString *temp = #".";
NSLog(#"%#", temp);
NSLog format specifiers:
%# Object, calls the Object's description method
%d, %i signed int
%u unsigned int
%f float/double
%p for pointers to show the memory address
%zu value of type size_t (for sizeof(variable function)
%s C strings
\u can used for arbitrary unicode chars example:NSLog(#"\u03c0");//π
As per my assignment I have to take in input from a user via a console to be used with NSString.
At the moment I have
char* name[100]; // declaring char array
NSString* firstName; // declaring the NSString
printf("Please enter first name \n");
printf("=> ");
scanf("%s", &name);
firstName = [NSString stringWithCString:name encoding:NSASCIIStringEncoding];
This works, however I am getting this warning
Incompatible pointer types sending 'char [100]' to parameter of type
'const char '
I don't want to be having these errors coming up in the code, I would like to also mention I'm using Xcode 4.2.
Can anyone explain to me why I'm getting these errors, and if I can possibly overcome them?
Many thanks in advance!
Change this:
char* name[100];
to
char name[100];
The first form creates an array of 100 pointers to char. The second one creates an array of 100 char elements. What might be confusing, is that name in that last case, is in fact a pointer, pointing to the first of these 100 char elements.
As printed with NSLog is assigned to initialize a NSString.
NSLog(#"%s", arrayChar);
NSString *str = [NSString stringWithFormat:#"%s", arrayChar];
NSLog(#"Array to String: %#",str);
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);
I'm not sure what's going on here. For some reason NSString seems unwilling to load in the double value d.
Here's my code:
-(NSString*)minuteFormat:(double) d{
NSString* mystring;
if(d >= 10){
mystring = [NSString stringWithFormat:#"%d", d];
}else{
mystring = [NSString stringWithFormat:#"0%d",d];
}
return(mystring);
}
Regardless of the value of d, the only thing that's getting returned is 0 or 00. (And I'm sure d is getting inputted correctly, as I've used breakpoints to check.)
Could someone tell me what's going on?
%d is for integers. You probably want %f. See String Format Specifiers
%d is a decimal integer format string. Try %lf for double.
You should use %f (double) like this
mystring = [NSString stringWithFormat:#"%f", f];
or do a typecast of the value. For this you may use [yourString floatValue]
Is there a general purpose function in Objective-C that I can plug into my project to simplify concatenating NSStrings and ints?
[NSString stringWithFormat:#"THIS IS A STRING WITH AN INT: %d", myInt];
That's typically how I do it.
Both answers are correct. If you want to concatenate multiple strings and integers use NSMutableString's appendFormat.
NSMutableString* aString = [NSMutableString stringWithFormat:#"String with one int %d", myInt]; // does not need to be released. Needs to be retained if you need to keep use it after the current function.
[aString appendFormat:#"... now has another int: %d", myInt];
NSString *s =
[
[NSString alloc]
initWithFormat:#"Concatenate an int %d with a string %#",
12, #"My Concatenated String"
];
I know you're probably looking for a shorter answer, but this is what I would use.
string1,x , these are declared as a string object and integer variable respectively. and if you want to combine both the values and to append int values to a string object and to assign the result to a new string then do as follows.
NSString *string1=#"Hello";
int x=10;
NSString *string2=[string1 stringByAppendingFormat:#"%d ",x];
NSLog(#"string2 is %#",string2);
//NSLog(#"string2 is %#",string2); is used to check the string2 value at console ;
It seems the real answer is no - there is no easy and short way to concatenate NSStrings with Objective C - nothing similar to using the '+' operator in C# and Java.