Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Best way to clear contents of NSTextView?

Best way to clear contents of NSTextView?
Thread Tools
Senior User
Join Date: Jan 2000
Status: Offline
Reply With Quote
Oct 25, 2001, 05:38 PM
 
I'm working on a small app (for the sake of learning Obj-C) that uses an NSTextView. When the user clicks on the lone button available, some things are written to the NSTextView.

I'd like to delete the original contents of the text field every time the button is clicked, replacing them with the new contents. As it stands now, the new text is merely appended to the old.

Any suggestions? At the moment, I'm using insertText: to add text to the NSTextView.
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Oct 25, 2001, 05:48 PM
 
Use [yourTextView setString:""];

Atleast i think that's what it was...

HTH,
F-bacher
     
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Oct 25, 2001, 07:20 PM
 
Originally posted by Ghoser777:
<STRONG>Use [yourTextView setString:""];
</STRONG>
<font face = "courier">
shouldn't that be [yourTextView setString:@""];
</font>
...?

[edit: my way of marking the change didn't work, note the "@" behind the colon]

[ 10-25-2001: Message edited by: seb2 ]
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Oct 25, 2001, 09:17 PM
 
Bah, I'm a java guy learning Obj-C, we forget @'s every once in a while

Sorry,
F-bacher
     
fitter  (op)
Senior User
Join Date: Jan 2000
Status: Offline
Reply With Quote
Oct 25, 2001, 10:14 PM
 
Thanks much to you both. That's what I was looking for.
     
Forum Regular
Join Date: Aug 2001
Location: Vienna, Austria
Status: Offline
Reply With Quote
Oct 26, 2001, 06:14 AM
 
Forgetting the @ is pretty bad. It just produces a warning at compile time, but it's a bad crash (SIGBUS) at runtime. I already spend HOURS by looking for a memory problem in my app when all I did wrong was to forget an @.
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Oct 26, 2001, 01:10 PM
 
Same here; It took me forever to figure out why all these @'s where in Objective-C code. String objects in Java are so much easier (and so much more intuitive for that matter).

F-bacher
     
Mac Enthusiast
Join Date: Sep 2001
Location: McKinney, TX
Status: Offline
Reply With Quote
Oct 26, 2001, 04:09 PM
 
Originally posted by Ghoser777:
<STRONG>Same here; It took me forever to figure out why all these @'s where in Objective-C code. String objects in Java are so much easier (and so much more intuitive for that matter).

F-bacher</STRONG>
You can always avoid the "@" notation if it annoys you. Just use

[yourTextView setString:[[NSString alloc] initWithCString:""]];

[ 10-26-2001: Message edited by: TheBum ]
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Oct 27, 2001, 03:10 AM
 
Actually, that's a memory leak. You'd need:

[yourTextView setString:[NSString stringWithCString:""]];

or just

[yourTextView setString:[NSString string]];

but of course the @"" is easiest and best.

Since Objective-C is an extension of ANSI C, regular old "" still has to mean constant c-strings, so ObjC has to use @"" to represent constant NSString objects. Slightly annoying, but you can get used to it. Just don't ever ignore those compiler warnings :-)
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Oct 28, 2001, 06:23 AM
 
Well, @"" is nice when you compare it to CFSTR(""), which is how you declare constant CFStrings
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:53 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2