# Can anyone solve my problem (Cocoa)?



## rhale1 (Aug 6, 2002)

I've looked over my code but can't seem to find the error. "boldText" is the contents of an NSTextView. I want the contents to be placed in a string so that I can add the string to "theText" and insert it into my main document.

What happens is IB gives me a strange error. I'm attaching an image.

```
- (IBAction)addBoldText:(id)sender
{
    NSMutableString *boldText;
    NSString *theText;
    NSRange theRange;
    NSRange rangeToSelect;

    boldText = [theTextView string];
    
    // build the string, trivial
    theText = [NSString stringWithFormat:@"<b>%@</b>", boldText];

    // Here we force the text view to become the first responder, so that the
    // selection is visually displayed properly if there is no selection
    [[theTextView window] makeFirstResponder:theTextView];

    // insert the string that we've built above
    [theTextView insertText:theText];

    // get the current selected range (which will be right after
    // the just inserted text)
    theRange=[theTextView selectedRange];

    // calculate the range that will contain the text that is contained within the
    // <A></A> construct that we've just built.  Odds are that the user may want to edit it.
    // you may want to modify this so that holding down different modifier keys will
    // provide you with different selection behaviours
    rangeToSelect.location=(theRange.location-4)-[boldText length];
    rangeToSelect.length=[boldText length];
    [theTextView setSelectedRange:rangeToSelect];

    // we're done!  So we need to return YES
    // returnValue=YES;
}
```

PS: This is an HTML Editor. That may be obvious due to the "b" tags.


----------



## Ghoser777 (Aug 7, 2002)

Hey there.  First of all, I'm assuming you mean PB, as you don't really compile anything or get messages lime that in IB.

I believe this line of code should give you issues:


```
boldText = [theTextView string];
```

That's because boldText is of type NSMutableSrting (why?) and [theTextView string] returns an object of type NSString.  Yes, NSMutableString is a subclass of NSString, but I always get errors when I do something like this.  (Doesn't this work in Java?  I swore Sublcass instance variables could be pointed to superclass objects.)  Considering you don't even edit boldText, You probably should just change the type from NSMutableString to NSString.  If you do plan on editing it, you might want to to this:


```
boldText = [NSMutableString stringWithString:[theTextView string]];
```

Another thing to keep in mind, I believe that the string obtained from NSTextView's change if you edit the test in the NSTextView.  That is, if you get [theTextView string] and then an end user edits the contents of the text view, then the string reference you have is altered as well.

Quick fix is to just send it a copy message.

HTH,
Matt Fahrenbacher

P.S. And go try out Alarm Clock S.E. Beta 3.0! =)


----------



## rhale1 (Aug 7, 2002)

Thanks! Also, why I was walking to Blockbuster, I realized that I was calling 
	
	



```
[theTextView string]
```
 as opposed to 
	
	



```
[boldTextView string]
```
 that I wanted to code. Wow, it must have been late when I coded this.

I'll try AlarmClock SE. I need an alarm clock for my iMac.


----------

