Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Strange NSString / NSTextField issue

Posted on 2013-05-27
5
Medium Priority
?
535 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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:   …
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

783 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