Solved

Strange NSString / NSTextField issue

Posted on 2013-05-27
5
507 Views
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"
(lldb)

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?
0
Comment
Question by:kevinvw1
[X]
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
5 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
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];
0
 

Author Comment

by:kevinvw1
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"
0
 
LVL 33

Accepted Solution

by:
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
0
 

Author Comment

by:kevinvw1
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.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 39202413
You are welcome.
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

624 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