NSString cutting one string to pieces - objective-c

So, I am making an item class to my project and I want to have keywords to search it in my search tab, I read that you can't put arrays to coreData, so I thought I put a string and seperate keywords with ,
So how would I read that string then and cut it to smaller pieces? Or is there a better way to do this?

Look at NSString's -componentsSeparatedByString: and -componentsSeparatedByCharactersInSet: methods.

Related

Turning plain text into NSString

I have a list of words like: hello hi bonjour etc.
I'm literally adding #"" around every word manually. It's not bad, but when you have thousands of words it can get really tedious. Is there a method, where you input a bunch plain text like hello hi and it turns it into: #"hello" #"hi", which I can then copy into code, or do I have to do this manually?
the
be
and
of
a
What I've done in similar cases is use an editor's text replacement feature to replace all blanks with (in your case) " #". You may have to get a bit fancier if you don't have blanks at the start and end of each line, or you have multiple adjacent blanks, etc, but the general technique works.
It sounds like what you want is NSString's componentsSeparatedByString: method. You can enter in a bunch of plain text into some text field or text view, get the raw string, and then push that through "componentsSeparatedByString:#" "" (make sure there's one space in there) and out comes an array of words.
You could try loading the strings from a Plist -- inserting them into code is kind of ugly. Something like [NSDictionary dictionaryWithContentsOfURL:urlToMyPlist] or [NSArray arrayWithContentsOfURL:urlToMyPlist]
Look at the docs for NSString and you'll find the -initWithCharacters:length: method, which you can use to create strings. If you have a string that you want to break up into smaller strings, use -componentsSeparatedByString:.

Cocoa - Search field with Plist

I am sorry to ask this but I have searched for hours on doing this but I really don't understand it. Please help me. I have a .plist file in my Xcode project and it's root is a Dictionary type. It contains about 50 more dictionaries. Inside the dictionary contains strings. (Dictionary(root) > Dictionary > String. I added a search field to my toolbar and linked it to my code. I am able to get what the user types but then how do I "search" after getting what the user typed? Is there a method for this and how do I link it into my .plist? Thank you so much!!!
You want to search for the user entered string in your Dictionary of Dictionaries?
You're going to have to iterate each dictionary, asking [dict objectForKey:userEntry] in each. Not sure if you want to only find first match or all matches too.
Additionally, you may want to create an abstraction of your Dictionary of Dictionaries to reduce the scale of the problem and clarify the API. In simpler terms, wrap your Dictionary of Dictionaries in a class and put a sensible (non-dictionary-based) set of methods on it. It's probably worth the effort.
To load the plist into a Dictionary, look at [Dictionary dictionaryWithContentsOfFile].
Edit: Filtering options on NSDictionary
Have you looked at the following options for filtering values in an NSDictionary:
[NSDictionary keysOfEntriesPassingTest:] (10.6 and later) or
take the [rootDictionary allValues] NSArray and use Predicates, perhaps like this.

NSString and NSMutableString concatenation

I have three strings (a NSString, a NSMutableString, and another NSString) which I need to concatenate into a mutable string, in that order, to display as the source for a UIWebView. Comming from a PHP/JavaScript/HTML background, my knowledge of concatenation is pretty much this:
var concatenatedString = string1 + string2 + string3;
I presume that sort of thing won't work in Objective-C, so I'm wondering how to go about pulling them all together properly.
To give a bit of setting for this, the first string (NSString) is the header and canvas element of a web page, the second string (NSMutableString) is javascript from a text field that the user can define to manipulate the canvas element, and the third string (NSString) is the end tags of the web page.
Also, rather than initially creating the NSMutableString, should I just referance the UITextView.text to the get the user's text when concatenating the whole thing, or should I pull the text from the UITextView first?
NSMutableString *concatenatedString = [[NSString stringWithFormat:#"%#%#%#", string1, string2, string3] mutableCopy];
The other two answers are correct in that they answer the question as you asked it. But by your description of what you want to do there is a much easier way. Use a format.
Assuming string1 and string3 will always be the same and only string2 will change,which is what it sounds like you are doing you can write something like this.
static NSString *formatString = #"String1Text%#String3Text";
NSString *completeString = [NSString stringWithFormat:formatString,self.myTextFieldName.text];
NSLog(#"%#",completeString);
The %# in the format says to insert the description of the object following the format.(The description of an NSString is the stringValue.)
Assuming you have a UITextField named myTextFieldName, that currently contains the text 'String2Text' Then this will be the output:
'String1TextString2TextString3Text'
In this way you only create 1 instance of an NSString format for the whole class no matter how many times you call this code.
To me it sounds like you don't need a mutable string at all. Feel free to leave a comment if I misunderstood anything.
Response to comment:
I'm not sure how you are implementing 'moves to test it out again' but, let's say you have a button named 'testJavaScript'. The IBAction method connected to that button would have the first two lines in it. So each time you pushed the button it would make a new formatted NSString filled with the current contents of the textfield. Once this string was formed it could not be changed. But it won't matter since next time it will make another.
NSString *concatenatedString = [string1 stringByAppendingFormat:#"%#%#", string2, string3];
You can make the resulting string mutable (if you really need to) by adding mutableCopy as shown in the answer by #Vinnie.

How to do string manipulation in an NSValueTransformer?

I have a table column that I'm binding to a value in an NSArrayController. What I'm trying to do is display only a substring of the actual value in the array controller. The way I've been trying to do that so far is by creating an NSValueTransformer subclass, and then doing the string manipulation in the transformedValue method. However, I can't figure out how to get the incoming value turned into a string (it's of type NSConcreteValue), and maybe there's an easier way to do this without value transformers.
Sounds like you're doing presentation-side formatting, in which case you should be using a formatter instead.
Then again, if this is a string containing multiple values (e.g., something like “from 42 to 100”), you should make a model object from it instead, and store those in the array controller. Then you can bind your table columns to specific properties of the model objects, and not have to worry about picking the string apart and then reassembling it (except when you load and later save the model).
Edit: Never mind; I didn't see that the object values are NSValues, not NSStrings.
You can get a string representation of any object using the -description method, but for instances of NSValue it's unlikely to print anything especially meaningful. In other words, it's up to your value transformer to interpret the passed-in object and produce a string. If it's an NSValue instance, the question is what type of data that instance contains. Once you know that, you can write code to represent it as a string (similar to NSStringFromRect()).

Arrays in Objective-C

If you want to use an array of characters/badguys is it better to have one array of badguy objects and each object have properties like Badguy1: color=blue, health=80. Then you loop through your array of characters and pull that information.... OR is it better to have multiple small arrays like character array, color array, health array and the index of all 3 arrays align to be the same character?
I know how to pull information from each array if it is separate but I do not know how to get the properties of each character if it is all wrapped up in 1 array.
I ask just because it seems like it would make more sense to use a single array and pull the parts that you need.
I know how to pull information from each array if it is separate but I do not know how to get the properties of each character if it is all wrapped up in 1 array. I ask just because it seems like it would make more sense to use a single array and pull the parts that you need.
You are correct in that it's generally smarter and a better design decision to keep related things together. If you'd like to store them this way, you can simply do:
// Get the character at index 3 in the "characters" array
// and print how many hit points it has left.
GameCharacter *ch = [characters objectAtIndex:3];
NSLog(#"This character has: %# hit points", ch.hitPointsRemaining);
Actually, now that I think about it. I guess I should just have an array of pointers and then get my values from the objects and not try to store all of that in the array.