Objective-c integer to string giving random numbers? - objective-c

I'm busy working on an iPad application and my web service returns pretty simple JSON data. All seems well and I have other methods doing this same conversion without issue however, I have 1 method that returns a random string when doing a integer -> string conversion.
My userdata object below is a NSDictionary created by the SBJSON parser. The value when debugging of [userdata objectForKey:#"UserID"] is 1.
However when I do this
NSString *userId = [NSString stringWithFormat:#"%d", [userdata objectForKey:#"UserID"]];
The value in userId is or appears to be a random number such as 23425234. I also tried the %d in my format but got the same result.

Because it is an object, not an int, you see the address of the object, instead, you can do that:
NSString *userId = [NSString stringWithFormat:#"%#", [userdata objectForKey:#"UserID"]];
^

Try with:
NSString *userId = [NSString stringWithFormat:#"%d", [[userdata objectForKey:#"UserID"] intValue]];
Or:
NSString *userId = [NSString stringWithFormat:#"%#", [userdata objectForKey:#"UserID"]];
object for key is probably NSNumber and not int...

Related

Objective-c how to convert NSURL into NSString?

Total objective-c noob here with a question.
Is there a way to convert NSURL into NSString in one line?
I need to retrieve URL from sqlite database abd then save it into string.
Currently the line i want to convert looks like this ->
MyString.url = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 1)];
so ofcourse im getting 'Incompatible pointer types assigning to 'NSURL *' from 'NSString *''
:)
Try This :
NSString *aStrUrl = [aUrlObj absoluteString];
You can use absoluteString property of NSURL
Example:
NSString *urlString = [url absoluteString];
In Swift
var urlString = url.absoluteString
If you're working on swift than use :
var urlStr : String = myUrl.absoluteString
You can use any one
NSString *string=[NSString stringWithFormat:#"%#",url1];
or
NSString *str=[url1 absoluteString];
NSLog(#"string :: %#",string);
string :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAAA1F7476071FE/DuplicateMedia.app/loading_circle_animation.gif
NSLog(#"str :: %#", str);
str :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAA-A1F7476071FE/DuplicateMedia.app/loading_circle_animation.gif

Making a backspace button for a calculator

I am making an iOS calculator and it I'm having minor difficulties with the backspace button (for deleting the last number of the value displayed on a label).
To get the current value on the label I use
double currentValue = [screenLabel.text doubleValue]
Following other questions, I tried something like
-(IBAction)backspacePressed:(id)sender
{
NSMutableString *string = (NSMutableString*)[screenLabel.text];
int length = [string length];
NSString *temp = [string substringToIndex:length-1]
;
[screenLabel.text setText:[NSString stringWithFormat:#"%#",temp]];
}
But it does not work,
(Xcode says "setText is deprecated", "NSString may not respond to setText" and that an identifier is expected in the first line of code inside the IBAction)
and I do not really understand this code to make it work by myself.
What should I do?
It should be
[screenLabel setText:[NSString stringWithFormat:#"%#",temp]];
Your Xcode clearly says that you are trying to call setText' method on anNSStringwhere as you should be calling that on aUILabel. YourscreenLabel.textis retuning anNSString. You should just usescreenLabelalone and should callsetText` on that.
Just use,
NSString *string = [screenLabel text];
The issue with that was that, you are using [screenLabel.text]; which is not correct as per objective-c syntax to call text method on screenLabel. Either you should use,
NSString *string = [screenLabel text];
or
NSString *string = screenLabel.text;
In this method, I dont think you need to use NSMutableString. You can just use NSString instead.
In short your method can be written as,
-(IBAction)backspacePressed:(id)sender
{
NSString *string = [screenLabel text];
int length = [string length];
NSString *temp = [string substringToIndex:length-1];
[screenLabel setText:temp];
}
As per your question in comments(which is deleted now), if you want to display zero when there are no strings present, try,
-(IBAction)backspacePressed:(id)sender
{
NSString *string = [screenLabel text];
int length = [string length];
NSString *temp = [string substringToIndex:length-1];
if ([temp length] == 0) {
temp = #"0";
}
[screenLabel setText:temp];
}

String to UTF8 Char Conversion in Objective-C

I'm trying to save an UTF8 char to a string and print it to a label.
If I hard code it works fine:
NSString *param = #"\uf02e";
NSLog(param);
Result:
2012-10-24 16:09:56.522 i[22996:12c03] 
By the way if I'm saving the char to a string I can't go back.
NSString *myString = [NSString stringWithFormat:#"%#",[item objectForKey:content]];
NSLog(myString);
Result:
2012-10-24 16:18:47.289 i[23105:12c03] \uf02e
Any solution for this? Thanks.
EDIT
item is an NSDictionary and [item objectForKey:content] is a string.
NSString *param = #"\uf02e";
NSDictionary* item = [NSDictionary dictionaryWithObject: param forKey: #"key"];
NSString *myString = [NSString stringWithFormat:#"%#",[item objectForKey: #"key"]];
NSLog(myString);
Works fine for me. So the error is in the value, that you're inserting into the dictionary.

Simple int to NSString conversion as in Java?

Is there any simple way how to initialize String in Objective-C with int such as in Java:
String myStr = 42 + "";
or I have to do
[NSString stringWithFormat:#"%d", 42];
everytime?
You could also use the NSNumber class for that:
NSNumber *number = [[NSNumber alloc] initWithInteger: val];
NSString *string = [number stringValue];
Perhaps not shorter, but it could be eventually faster.
Also you could create as said a helper method, than you wouldn't have to use more code than with the stringWithFormat: method.
Yes you have to do
[NSString stringWithFormat:#"%d", 42];
for Integer to string conversion.
Using a constant, like 42 in your example, you can write
NSString *myString = #"42";
Using a variable or expression, you can write
[NSString stringWithFormat:#"%d", myValue];

Merge 5 NSStrings in Objective-C

I have multiple NSStrings and i wish to merge them into one other, here is my code so far...
NSString *newURL = [_parameters objectForKey:#"url"];
NSString *emailBody = #"Hey!<br>I just snipped my long url with My Cool App for iPhone in just a few seconds!<p><b>"+newURL+#"</b></p>";
If you know the number of your existing strings, you can just concat them:
NSString* longString = [firstString stringByAppendingString:secondString];
or:
NSString* longString = [NSString stringWithFormat:#"A string: %#, a float: %1.2f", #"string", 31415.9265];
If you have an arbitrary number of strings, you could put them in an NSArray and join them with:
NSArray* chunks = ... get an array, say by splitting it;
NSString* string = [chunks componentsJoinedByString: #" :-) "];
(Taken from http://borkware.com/quickies/one?topic=NSString)
Another good resource for string handling in Cocoa is: "String Programming Guide"
You can try
NSString *emailBody = [ NSString stringWithFormat: #"Hey!<br>I just snipped my long url with Snippety Snip for iPhone in just a few seconds, why not check it out?<p><b>%#</b></p>", newURL ];
Given that you've got multiple strings I recommend using an Array:
NSArray *array = [NSArray arrayWithObjects:#"URL", #"person", "body"];
NSString *combined = [array componentsJoinedByString:#""];
Formatting string has better readability and less error-prone:
NSString *newURL = [_parameters objectForKey:#"url"];
NSString *emailBody = [NSString stringWithFormat:#"Hey!<br>I just snipped my long url with Snippety Snip for iPhone in just a few seconds, why not check it out?<p><b>%#</b></p>", newUrl, newUrl];
You can concatenate strings in Cocoa using:
[NSString stringByAppendingString:]
Or you could use the [NSString stringWithFormat] method which will allow you to specify a C-style format string with a variable argument list to populate the escape sequences.