Right. What I couldn't figure out was how to change that NSTextField (which comes in as type IBOutlet; internally it is type NSTextField) to an NSMutableString so that I could do string methods like "length" on it directly. I suppose I could just do a
to cast it, and use myString from then on. I don't know if that is the preferred way of doing it though.
IBOutlet isn't a type, it's a keyword that is ignored by the compiler but scanned by Interface Builder.
That is the only way to do it. NSTextField doesn't handle stuff that NSString is responsible for.
Also, you're not casting anything (well, actually, you are, but you shouldn't be and I'll get into that in a bit). You're just accessing the text field's string value, and storing it. The string is a completely different object than the text field.
About the your accidental casting: stringValue doesn't return an NSMutableString, but an NSString. That code should generate a warning on compile, but either way, you're not getting a mutable string back, just a regular string. So if you pass any message that an NSString can't handle, your app is gonna throw an exception (and/or crash).
Cool. Thanks for the explanation. I'm gonna go make the code more elegant now.
I do need a mutable string because I need to sort the letters in the word alphabetically as a hash, and I have to either make it all lowercase or all uppercase before sorting (in case the user enters mixed case text) so that the letters will sort properly. I chose to lowercase it, sort the letters and then uppercase it for output.
That could be made simpler I suppose by some filter function on input to force uppercase, or failing that, by just uppercasing it before sorting and leaving it that way through the output. Amazing the improvements you always find no matter how many times you go back and re-look at code.
Cool. Thanks for the explanation. I'm gonna go make the code more elegant now.
I do need a mutable string because I need to sort the letters in the word alphabetically as a hash, and I have to either make it all lowercase or all uppercase before sorting (in case the user enters mixed case text) so that the letters will sort properly. I chose to lowercase it, sort the letters and then uppercase it for output.
That could be made simpler I suppose by some filter function on input to force uppercase, or failing that, by just uppercasing it before sorting and leaving it that way through the output. Amazing the improvements you always find no matter how many times you go back and re-look at code.
Also, don't forget that you can call lowercaseString or uppercaseString on an NSString.
I'm still not sure exactly what you're doing, but you might also be interested in NSCharacterSet.
I'll check it out. I need to sort the letters in a word into alphabetical order - this is my hash for retrieval. It works fine using a separate heap sort routine, but I am always fiddling with it and wonder if there is any way to transfer a string's characters as elements of an NSArray (so that I can then sort the array) without looping through the string character-by-character. Once I get the characters in an NSArray, it's a one-shot deal to sort the array - the problem is assigning a string to an array such that the array elements are the characters of the string. It's just fiddling to see if there is something else I can learn - it works fine as is, and it worked fine in the first iteration when I used NSEnumerator to loop through the string and assign the array elements one at a time.
100% agreed - that's the beauty of it. Need to optimize away some of those method calls? Realize you *can* get away with raw functions in speedy speedy C? Do it. Being able to drop back to C at a moment's notice is a godsend.
Otherwise it's just Smalltalk.
Back in the day you would program in C and if you needed speed you would use Assembly, but now you use Obj-C and if you need speed you stop using objects.
Comments
Right. What I couldn't figure out was how to change that NSTextField (which comes in as type IBOutlet; internally it is type NSTextField) to an NSMutableString so that I could do string methods like "length" on it directly. I suppose I could just do a
NSMutableString *myString = [JumbleWord1 stringValue];
to cast it, and use myString from then on. I don't know if that is the preferred way of doing it though.
IBOutlet isn't a type, it's a keyword that is ignored by the compiler but scanned by Interface Builder.
That is the only way to do it. NSTextField doesn't handle stuff that NSString is responsible for.
Also, you're not casting anything (well, actually, you are, but you shouldn't be and I'll get into that in a bit). You're just accessing the text field's string value, and storing it. The string is a completely different object than the text field.
About the your accidental casting: stringValue doesn't return an NSMutableString, but an NSString. That code should generate a warning on compile, but either way, you're not getting a mutable string back, just a regular string. So if you pass any message that an NSString can't handle, your app is gonna throw an exception (and/or crash).
So your code should either be
NSString *string = [jumbleWord stringValue];
-or-
NSMutableString *mutableString = [[jumbleWord stringValue] mutableCopy];
(using the latter, of course, only if you actually need a mutable string)
I do need a mutable string because I need to sort the letters in the word alphabetically as a hash, and I have to either make it all lowercase or all uppercase before sorting (in case the user enters mixed case text) so that the letters will sort properly. I chose to lowercase it, sort the letters and then uppercase it for output.
That could be made simpler I suppose by some filter function on input to force uppercase, or failing that, by just uppercasing it before sorting and leaving it that way through the output. Amazing the improvements you always find no matter how many times you go back and re-look at code.
Cool. Thanks for the explanation. I'm gonna go make the code more elegant now.
I do need a mutable string because I need to sort the letters in the word alphabetically as a hash, and I have to either make it all lowercase or all uppercase before sorting (in case the user enters mixed case text) so that the letters will sort properly. I chose to lowercase it, sort the letters and then uppercase it for output.
That could be made simpler I suppose by some filter function on input to force uppercase, or failing that, by just uppercasing it before sorting and leaving it that way through the output. Amazing the improvements you always find no matter how many times you go back and re-look at code.
Also, don't forget that you can call lowercaseString or uppercaseString on an NSString.
I'm still not sure exactly what you're doing, but you might also be interested in NSCharacterSet.
http://cocoadevcentral.com/articles/000081.php
"This tutorial explains the parts of C you need to get started with Cocoa."
And it's even on topic!
100% agreed - that's the beauty of it. Need to optimize away some of those method calls? Realize you *can* get away with raw functions in speedy speedy C? Do it. Being able to drop back to C at a moment's notice is a godsend.
Otherwise it's just Smalltalk.
Back in the day you would program in C and if you needed speed you would use Assembly, but now you use Obj-C and if you need speed you stop using objects.