Solved

Strange NSString / NSTextField issue

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 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