Output to UITextField - ios

I have a NSDictionary with some parameters i want to display in a UITextField.
but
firstname.text = [userdata objectForKey:#"firstname"];
throws an exeption.
If i use NSLog on [userdata objectForKey:#"firstname"]; it shows the right value. What seems to be the problem here?

Just use the following code:
firstname.text = [NSString stringWithFormat:#"%#",
  [userdata objectForKey:#"firstname"]];
go on .. :)

When you NSLog something it uses [object description] to convert the object to a string for output. If the object stored in your dictionary is not an NSString and you try to assign to firstname.text you will get an exception.
Another problem you may be facing is your firstname object may not be pointing at what you think it is. This may occur, for example, if you are not using ARC and you forgot to retain it or you already released it.
The exact exception you are seeing will determine which of these you are encountering.

Tried to do something like
[NSString stringWithFormat:#"%#",[userdata objectForKey:#"firstname"]];
firstname.text = nameString;
?

Related

Use existing string as valueForKey value

I am trying to use a string ("levelNumberString") as a valueForKey in an NSDictionary. When I use a string like #"1", it all works perfectly, but when I decide to swap out that kind of string for levelNumberString, it gives me a SIGABRT error. The integer levelInt is an integer that decides what level the app is at. When the view is loaded, it is worth 1, and its value is taken up by the "levelNumberString" string.
This is my code (it's in viewDidLoad, if that helps at all):
NSString *levelNumberString = [NSString stringWithFormat:#"i%", levelInt];
NSDictionary *packDict = [NSDictionary dictionaryWithDictionary:[rootDict valueForKey:levelNumberString]];
The above code is what is giving me the error.
Thanks in advance!
The problem is over here #"i%" it should be #"%i"
NSString *levelNumberString = [NSString stringWithFormat:#"%i", levelInt];
NSDictionary *packDict = [NSDictionary dictionaryWithDictionary:[rootDict valueForKey:levelNumberString]];

How can I concatenate the string that I'm using to define this label text

this seems like it is probably dead simple to somebody but I'm stuck.
I have this little app that picks a random thing from an array of things and then displays it to the user in the form of a question.
NSLog(#"How about %#?", theThing);
// How About...
self.theThingLabel.text = theThing;
//How do I add a question mark to the end of that string
in the code above the NSLog string works just like I want the label to work. I take care of the "how about" part as a string above the displayed result, but I can't figure out how to add that question mark .. something like theThing+"?"
I tried a bunch of stuff but instead of getting lucky I got warned by xcode over and over.
Try this
self.theThingLabel.text = [theThing stringByAppendingString:#" ?"];
self.theThingLabel.text = [NSString stringWithFormat:#"How about %#?",theThing];
This is very easy
Use this :
self.theThingLabel.text = [NSString stringWithFormat:#"How about %# ?", theThing];
theThing = [NSString stringWithFormat:#"How about %#?",theThing];
NSLog(#"%#", theThing);
then
self.theThingLabel.text = theThing;
Hope it helps..
NSString *what= #"What";
NSString *finalString = [NSString stringWithFormat:#"How about %# ..?", what];
NSLog(#"%#", finalString);
Hope that helps

value with apostrophe are not inserted correctly in NSmutableDictionary [duplicate]

This question already has an answer here:
Unicode characters don't display properly in NSLog output
(1 answer)
Closed 9 years ago.
I want to create a dictionary with key "name" and number with value "George’s iPhone" and "123456".
My code is:
NSString *name = #"George’s iPhone";
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:#"123456" forKey:#"number"];
[dict setValue:name forKey:#"name"];
but i got,
name = "George\U2019s iPhone";
value = "123456";
There's nothing wrong with your code, nor is there anything wrong with the output.
\U2019 is an escape sequence that works out to the desired character.
Now, you probably wanted ', and not ’, but that's really neither here nor there.
When you are doing the initialization of your string, NSString is picking up the correct string encoding for you which is probably UTF8. So your apostrophe is \U2019 in unicode. Nothing wrong in your code: what you are seeing in debugger or an NSLog is correct here.
You used:
NSLog(#"%#", dict);
That's why it shows like the output. In NSDictionary, the ' character is encoded.
You can check with:
NSLog(#"%#", [dict valueForKey:#"name"]);
It'll print the desired output.
Try like this:-
NSDictionary *yourDict=[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#"George’s iPhone",#"123456",nil] forKeys:[NSArray arrayWithObjects:#"name",#"number",nil]];
NSLog(#"%#",[[yourDict valueForKey:#"name"]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]);
NSLog(#"%#",[[yourDict valueForKey:#"number"]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]);

Difference between nil and #"" in NSMutableString iOS

I want to know the difference between nil and #"" in NSMutableString.
I need to clean string value in NSMutableString every second.
So
myMutableString = nil;
or
myMutableString = #"";
Which one is better to clean and why?
UPDATE
In the case of a mutable string, you have to alloc/init it first like this:
NSMutableString *myMutableString = [[NSMutableString alloc] init];
Maybe you have done that, but then you have to reset the string like this:
[myMutableString setString: #""];
So instead of writing myMutableString = #"", use the code above.
If you assign myMutableString to nil it is not a valid pointer/object and cannot respond to messages or actions. If you actually set it to #"", it is a totally valid object which can respond to messages, methods and actions, it is just contains a string with a length of 0.
myMutableString = nil;
[myMutableString appendString: #"It now contains a valid string!"];
This cannot happen since the string is nil
myMutableString = #"";
[myMutableString appendString: #"It now contains a valid string!"];
This can happen, myMutableString is a valid object and can respond to messages. And guess what, it now has a string!
So, a string object can still be initialized and have have an actual string value without any characters. Just like an array can be valid and have 0 objects inside it. Otherwise, how would you add to it!?
However, In an NSMutableString's scenario, you may have to actually alloc-init it.... somebody please clarify.
Obviously, assigning to #"" is better, it actually depends on your scenario though. I don't know why you would want to assign to nil, unless you are reassigning the variable to a new string object.
None of them
use
[myMutableString setString: #""];
to reset your string.
Your object remains the same. You invoke a method that clear its content.

EXC_BAD_ACCESS when using stringWithFormat?

While deploying my application, I got the error message: "Thread 1:Program received signal: "EXC_BAD_ACCESS".
My code is below:
-(NSDictionary *)syncWithList:(NSInteger)listID
{
NSString *urlit = [NSString stringWithFormat:#"http://0.0.0.0:3000/lists/%#/syncList.json?auth_token=%#",#"xxxxxxxxxxx",listID];
// **Here I got the error message: "Thread 1:Program received signal: "EXC_BAD_ACCESS"**
NSLog(#"url: %#",urlit);
NSURL *freequestionurl = [NSURL URLWithString:urlit];
ASIHTTPRequest *back = [ASIHTTPRequest requestWithURL:freequestionurl];
[back startSynchronous];
self.listData = [[back responseString] objectFromJSONString];
NSLog(#"%#",listData);
NSDictionary *dicPost = [listData objectAtIndex:0];
return dicPost;
}
Thanks a lot!!!!
You must not format NSInteger (which is just a typedef'd int on current iOS versions) with the %# specifier. Writing %# in a string format basically means "call description on the object and use the result".
But NSInteger is not an object, it's a primitive type.
You get a memory exception because when listID is 42 you access an object at memory address 42. This is definitely not what you want.
-(NSDictionary *)syncWithList:(NSInteger)listID
^^^^^^^^^
NSString *urlit = [NSString stringWithFormat:#"http://0.0.0.0:3000/lists/%#/syncList.json?auth_token=%#",#"xxxxxxxxxxx",listID];
^^
just use the %i format specifier instead of %# for listID.
NSString *urlit = [NSString stringWithFormat:#"http://0.0.0.0:3000/lists/%#/syncList.json?auth_token=%i",#"xxxxxxxxxxx",listID];
EDIT: So used to getting errors from Xcode without it giving me any clues I neglected to notice that the troubled line was already know. I'll leave this here in the hope it helps someone in future.
Try creating an exception breakpoint, it may point to straight to the line where your code is falling over which should help you figure out the problem.
Switch to the breakpoint 'tab' in the left hand navigator.
Click the little '+' at the bottom.
Create a breakpoint as shown in the image:
Run your code and see where it pops.
You used wrong data type to print out.
NSLog(#"%#",listData);
You made very popularar mistake in this line
NSString *urlit = [NSString stringWithFormat:#"http://0.0.0.0:3000/lists/%#/syncList.json?auth_token=%#",#"xxxxxxxxxxx",listID];
Second argument is of type NSInteger, but in format you use %#, this is object only, and compiler thinks that your listID is address of object.
Correct format is %li:
NSString *urlit = [NSString stringWithFormat:#"http://0.0.0.0:3000/lists/%#/syncList.json?auth_token=%li",#"xxxxxxxxxxx",listID];

Resources