Strange NSString / NSTextField issue

Posted on 2013-05-27
Last Modified: 2013-05-28
I have a completely basic cocoa osx app that has a text field and a button.

When you type something in to the text box and click the button it just sets a variable with the value in the text box -

- (IBAction)getText:(NSButton *)sender {    
    NSString *textFromField = _theText.stringValue;    

If I type less then 7 characters in to the text box the variable gets set correctly.
If I type 7 to 11 characters in the text box the variable has garbage at the end.

Here is a copy of the All Output window -

(lldb) print textFromField
(NSString *) $0 = 0x000000010241c060 @"12345"
(lldb) print textFromField
(NSString *) $1 = 0x0000000102425260 @"1234567\xd0\xc4h$\x10"
(lldb) print textFromField
(NSString *) $2 = 0x0000000100510a30 @"123456789.\x05\x10"
(lldb) print textFromField
(NSString *) $3 = 0x00000001022370d0 @"123456789012"

You can see that if I type 12345 in to the text box, the variable shows 12345
But if I type 1234567 the variable shows 1234567\xd0\xc4h$\x10
It shows the extra garbage until you get up to 12 characters then it is fine again.

Any ideas why this happens?
Question by:kevinvw1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 33

Expert Comment

ID: 39202255
Can you modify the method you posted and show the output?
- (IBAction)getText:(NSButton *)sender {    
    NSLog(@"text:%@", [[self theText] stringValue] );
    NSString *textFromField = [[self theText] stringValue];    

_theText is the NSTextField? It should be an outlet and so the text is here [[self theText] stringValue];

Author Comment

ID: 39202288
I changed it to use [[self theText] stringValue];

I added the NSLog and the output is correct.

But the variable still shows the strange characters at the end -

print textFromField
(NSString *) $0 = 0x0000000100145e60 @"abcdefg\xe0\xb1R\x01\x10"
LVL 33

Accepted Solution

pgnatyuk earned 500 total points
ID: 39202383
Add few more line to this method, for example:
- (IBAction)getText:(NSButton *)sender {    
    NSLog(@"text:%@", [[self theText] stringValue] );
    NSString *textFromField = [[self theText] stringValue];    
    NSLog(@"variable:%@", textFromField );

Open in new window

set a breakpoint in the last line and when the app will stop type in console:
po textFromField
I think the output will be correct.
po - print object. textFromField is an object of NSString class. So we should use po and not p in the debugger

Author Comment

ID: 39202393
Thanks for the info.

You are correct, po works.  It seems strange though that Auto window also shows the extra characters and when I hove the mouse over the variable in debug, it shows the extra characters.

Also,  I created a new text box and set it to the value of the variable and the new text box showed the correct text.

Finally, I did an "isEqualToString" and it confirmed that the variable was truly only holding the correct text and not the additional garbage.

Thanks again.  I am just learning Objective-C.
LVL 33

Expert Comment

ID: 39202413
You are welcome.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface   In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system. I've never seen this model. I did see this one:   …
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question