Solved

Strange NSString / NSTextField issue

Posted on 2013-05-27
5
486 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
  • 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now