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 > NSRange Issues

NSRange Issues
Thread Tools
Mac Elite
Join Date: May 2002
Location: New York City
Status: Offline
Reply With Quote
Mar 10, 2003, 03:21 PM
 
I'm trying to create an action which would take the selected text in an NSTextView (textView), and add tags around it.

Here's the code...
- (IBAction)wrapBold:(id)sender
{
NSRange selection = [self selectedRange];
NSString *wholeThing = [self string];

NSString *oldString = [wholeThing substringWithRange:selection];
NSString *newString = [NSString stringWithFormat:@"%@", oldString];
[self insertText:newString];
}
The part in red (creating the NSRange) returns an "invalid initializer" in the PB console.

I know it must be something simple (hopefully).

Anyone have any ideas?
     
Addicted to MacNN
Join Date: Jun 1999
Location: Las Vegas, NV, USA
Status: Offline
Reply With Quote
Mar 10, 2003, 03:35 PM
 
I don't think you want to send the message to self. How can a range have a selected range?

Chris
     
zachs  (op)
Mac Elite
Join Date: May 2002
Location: New York City
Status: Offline
Reply With Quote
Mar 10, 2003, 03:42 PM
 


So then I would change it to:
NSRange selection = [textView selectedRange];

Or not...?
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Mar 10, 2003, 03:57 PM
 
Is this method in your NSTextView subclass? Because it sounds like this isn't a subclass of NSTextView.

(I don't know what chabig is talking about, though; self should never refer to an NSRange, since it's incapable of receiving messages.)
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
Mar 11, 2003, 10:23 AM
 
Actually, I think that's all fine. Wat's missing is a * as in NSRange *selection, right?

Also, I don't have he docs on hand, but make sure that insertText just replaces your selection and not the whole field's contents.

And guys, either you didn't read carefully or you or I have a serious error in understanding messaging. In the code
Code:
NSRange *selection = [self selectedRange];
the "self" is the current object running the method (boldWrap or whatever), not the NSRange class object. So if this method is a subclass or category of NSTextView, then the code is ok, but if it's some controller class, then yes, [NSTextView selectedRange] is necessary.
(Last edited by Gametes; Mar 11, 2003 at 10:29 AM. )
you are not your signature
     
Forum Regular
Join Date: Aug 2000
Location: UK
Status: Offline
Reply With Quote
Mar 11, 2003, 11:12 AM
 
Originally posted by Gametes:
Actually, I think that's all fine. Wat's missing is a * as in NSRange *selection, right?
Nope. From the docs:

- (NSRange)selectedRange

So provided that self is an instance or subclass of NSText (which includes NSTextView) then the code is corrrect. However, from the error you are getting I would assume that self is not a subclass of NSText, and you mean to call [textView selectedRange].


Also, I don't have he docs on hand, but make sure that insertText just replaces your selection and not the whole field's contents.
I think there is a better way to do the text insertion code. It seems to get the selection, make a copy of it, and then set the selection to this new string. I think this would be a little more readable (unless I have misunderstood and you are trying to do something completely different):

Code:
NSRange selection = [self selectedRange]; NSString *wholeThing = [self string]; [textView replaceCharactersInRange:selection withString:[NSString stringWithFormat:@"<b>%@</b>", [wholeThing substringWithRange:selection]]];
(Last edited by calumr; Mar 11, 2003 at 11:18 AM. )
     
Forum Regular
Join Date: Jun 2001
Location: Savoy, IL USA
Status: Offline
Reply With Quote
Mar 11, 2003, 11:16 AM
 
Originally posted by Gametes:
Actually, I think that's all fine. Wat's missing is a * as in NSRange *selection, right?

Also, I don't have he docs on hand, but make sure that insertText just replaces your selection and not the whole field's contents.

And guys, either you didn't read carefully or you or I have a serious error in understanding messaging. In the code
Code:
NSRange *selection = [self selectedRange];
the "self" is the current object running the method (boldWrap or whatever), not the NSRange class object. So if this method is a subclass or category of NSTextView, then the code is ok, but if it's some controller class, then yes, [NSTextView selectedRange] is necessary.
No, NSRange is a struct, not an object. If you look in the documentation, you'll see that a lot of primatives are defined as structs (e.g. NSRect, NSPoint, NSRange, NSSize, etc.). Therefore, functions that return these structs return them by copy, and not by reference, and thus, not as pointers. The method call

Code:
NSRange selection = [self selectedRange];
is fine, as long as the boldWrap method is being defined in a subclass of NSTextView. Therefore:
Code:
// -=-=-=-=-= in MyTextView.h #import <AppKit/AppKit.h> @interface MyTextView : NSTextView { // Whatever members you add } - (IBAction) wrapBold: (id) sender; @end -=-=-=-=-=-= in MyTextView.m #import "MyTextView.h" @implementation MyTextView // Initializers & dealloc and misc stuff - (IBAction) wrapBold: (id) sender { NSRange selection = [self selectedRange]; NSString *wholeThing = [self string]; NSString *oldString = [wholeThing substringWithRange:selection]; NSString *newString = [NSString stringWithFormat:@"%@", oldString]; [self insertText:newString]; } @end
is legal and should compile fine. In fact, what I'll bet you're doing is following the example in Chapter 24 of Hillegass's fine book Cocoa Programming for Mac OS X®! What you may be missing is the sentence on page 331 which states:

"Now you will need to write the code that will swap our the NSTextView and swap in your SGMLView."
Which implies that the 'wrap:' method that you're writing will be part of the SGMLView subclass that you write.

If you're not following Hillegass, I would highly recommend it, since it's an awesome book.

Gametes: You're right about part of what you said.... self is the reference to the current object, as you said, but if this were in the controller class, you would have 'textView' as an outlet in your class:

Code:
-=-=-=-=-=-=- In SGMLController.h #import <AppKit/AppKit.h> @interface SGMLController : NSObject { IBOutlet SGMLView *textView; // Whatever else } - (IBAction) wrapText: (id) sender; @end -=-=-=-=-=-=- In SGMLController.m #import "SGMLController.h" @implementation SGMLController - (IBAction) wrapText: (id) sender { NSRange selection = [textView selectedRange]; NSString *wholeThing = [textView string]; NSString *oldString = [wholeThing substringWithRange:selection]; NSString *newString = [NSString stringWithFormat:@"%@", oldString]; [textView insertText:newString]; } @end
You wouldn't use [NSTextView selectedRange], since that would only work if - () selectedRange was a class method.

And, yes, insertText does this:

insertText:

- (void) insertText: (id) aString

Inserts aString into the receiver's text at the insertion point if there is one, otherwise replacing the selection. The inserted text is assigned the current typing attributes.

This method is the means by which typed text enters an NSTextView. See the NSInputManager class and NSTextInput protocol specifications for more information.

See Also: - typingAttributes
[emphasis added by me]. Hope this helps.
Software Architect, CodeTek Studios, Inc.

12" AlBook 867 (Combo drive) 640 MB/40 GB (work development machine) -- TiBook 400MHz/384MB/10GB (home machine)
CodeTek VirtualDesktop Pro: Power multitasking! -- DockExtender: Powerful, efficient launcher for Apps, Docs and everything else!
     
   
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 08:52 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