 |
 |
Replacing Strings With String
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
Hey guys,
Im trying to replace a string with a string, so for example, if I type in "hello", the text would be changed to "goodbye".
I did some research and found this method:
replaceOccurrencesOfString
So I tried this code in my textStorageDidProcessEditing: notification method:
Code:
NSMutableString *text = [[[docTextView string] mutableCopy] autorelease];
[text replaceOccurrencesOfString:@"hey" withString:@"bye" options:NSLiteralSearch range:NSMakeRange(0, [text length])];
I get no errors or anything, but nothing happens in the text view. I tried NSLogging the mutable string, and it seems to be fine.
Any ideas?
Thanks,
Oliver
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Why would operating on a mutable copy of the backing store do something to the text view?
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
Originally posted by Chuckit:
Why would operating on a mutable copy of the backing store do something to the text view?
Not sure what you mean, I've looked over the same type of source on mamasam, and this is how they all seem to do what I'm looking for.
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
From the code you posted, what you are doing should not affect the text view at all. You first obtain the text view's string with [docTextView string]. Then you make a copy of it with mutableCopy. You then perform a search-and-replace on this new string that you just created. But that string is completely unconnected to the text view. If you wanted its contents to be displayed in the text view, you'd have to use [docTextView setString:text].
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Originally posted by Chuckit:
From the code you posted, what you are doing should not affect the text view at all. You first obtain the text view's string with [docTextView string]. Then you make a copy of it with mutableCopy. You then perform a search-and-replace on this new string that you just created. But that string is completely unconnected to the text view. If you wanted its contents to be displayed in the text view, you'd have to use [docTextView setString:text].
Or if it's really a NSTextView (sounds like it's more likely to be a NSTextField) you could operate directly on its -textStorage (NSTextStorage inherits from NSMutableAttributedString).
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
setString: did it, thanks. I got a little confused  .
Now I have another little problem, If I try this code:
[text replaceOccurrencesOfString:@"£" withString:@"£" options:NSLiteralSearch range:NSMakeRange(0, [text length])];
If I type in £, nothing happens, but if I replace the £ with a word, it works perfectly. I guess this means something to do with encoding. I am using Unicode (UTF-8) and I don't see why it shouldn't work.
Thanks,
Oliver
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Constant strings don't work too well with non-ASCII characters. You'd have to either escape it as a unicode character or load it from a strings file.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Nov 2002
Location: Seattle, WA
Status:
Offline
|
|
not to mention your source files can become corrupted if they have a bunch of non-ASCII characters in them (well, it happened to me at least; the file wasn't lost, but my "©" mysteriously changed to something else when I wasn't looking)
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by Angus_D:
Or if it's really a NSTextView (sounds like it's more likely to be a NSTextField) you could operate directly on its -textStorage (NSTextStorage inherits from NSMutableAttributedString).
NSMutableAttributedString, however, does not inherit from NSMutableString and lacks many methods that the latter has (including the one Oliver is using).
Also, since NSTextField is just an NSControl wrapper for an NSTextView, any techniques applicable to one ought to be applicable to the other.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|