I have an NSArray of NSStrings which I need to iterate, and remove all of the underscores in each NSString - nsmutablearray

I have an application where I have an NSArray of NSStrings. The problem is that these NSStrings all have "_" between each word which I need to remove. Unfortunately I am not sure how to do this.
This is the code I am working with:
for (NSString *word in wordList) {
[word stringByReplacingOccurrencesOfString:#"_" withString:#" "];
[wordTypeList addObject:word];
}
where wordTypeList is an NSMutableArray. My output unfortunately is the contents of the array, with no change (i.e. underscores have not been removed). What is it that I'm doing wrong?

stringByReplacingOccurrencesOfString returns a new NSString *, it does not modify the existing one.
Change your code to the following:
for (NSString *word in wordList) {
NSString * newWord = [word stringByReplacingOccurrencesOfString:#"_" withString:#" "];
[wordTypeList addObject:newWord];
}

Related

Split NSString into multiple entries to NSMutableDictionary as key/value?

Ok, so I have this problem I have been spinning my head around for some time now.
I have a NSString like the following:
NSString* foo = #"Brand: [Ford], Model: [Focus], Color: [black]";
which I would like to transfer into a NSMutableDictionary with Brand, Model, Color as keys and Ford, Focus, black as values (without the brackets [] ), but cannot seem to find any solution to this. How do I come about accomplishing the given scenario?
Edit:
Right now I use
NSArray *stringComponents = [foo componentsSeparatedByString#","];
which gives me an array like
stringComponents = [#"Brand: [Ford]",
#"Model: [Focus]",
#"Color: [black]",];
that I need to get into the syntax proposed by Sam, but how?
NSDictionary is basically just an array that stores the values of the objects and keys, so you'd do something such as:
NSArray *keys = [NSArray arrayWithObjects:#"Brand", #"Model", #"Color", nil];
NSArray *objects = [NSArray arrayWithObjects:#"Ford", #"Focus", #"black", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];

Write to dictionary in plist works but then read dictionary from plist returns (null)

This is how I am accessing the dictionary in the plist in my viewDidLoad method:
NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:#"Level.plist"];
filePath = [documentsDir stringByAppendingPathComponent:fileName];
array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
dict = [array objectAtIndex:1];
This works fine, I then write to the dictionary like this:
score = [NSString stringWithFormat:#"%d", score.integerValue + 10];
[dict setObject:score forKey:#"Score"];
[dict writeToFile:filePath atomically: NO];
This works fine too, however once I return to this view and try to access the dictionary again in the viewDidLoad method it returns (null) for the dictionary.
This is because you are reading the file as an array, but then when you write it back out again, you're only writing out the dictionary, not the array that contains that dictionary. If you try to use NSMutableArray to read a plist file that has something other than an array at its root, that will result in it returning nil. So if you just replace dict with array in the last line of code, that should do the trick.

How to store last part of string?

I have a string as http://www.google.co.uk/ig/images/weather/partly_cloudy.gif.
I need to save only the partly_cloudy.gif .How do i select obly that part of string?
If you are sure that it represents a path then you can call the lastPathComponent method on it.
Usage
NSString * link = #"http://www.google.co.uk/ig/images/weather/partly_cloudy.gif";
NSLog(#"%#", [link lastPathComponent]);
Use the NSString function componentsSeparatedByString. For example, NSString *url = #"http://www.google.com/1/2/3/test.gif"; NSArray *components = [url componentsSeparatedByString:#"/"]; would give you an NSArray of all strings in between a '/' character.
NSString reference: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

NSString within a link

I have a UIWebView that loads a link, http://www.google.com/a/datacommsales.net. But I want to have the datacommsales.net part interchangable. What I would like it to be is http://www.google.com/a/stringOne, stringOne being the NSString, so I can set the value of the string and change the link without editing the code. But the link is inside quotes, #"http://www.google.com/a/datacommsales.net", so it doesn't recognize the string. How could I include the string's value as part of the link? Any help is appreciated.
EDIT
To be a little more specific, here's my code:
- (IBAction)refreshNow:(id)sender
{
NSString *variablePart = #"secondpart.com";
NSString *page = [NSString stringWithFormat:#"google.com/a/variablePart/docs"];
[webView loadHTMLString:page baseURL:nil];
}
How would I put the string variablePart in the link like that?
do you mean just something like
NSString *variablePart = #"secondpart.com";
NSString *url = [#"http://www.google.com/" stringByAppendingString:variablePart];
EDIT:
NSString *variablePart = #"secondpart.com";
[NSString stringWithFormat:#"google.com/a/%#/docs", variablePart];
Checks the NSString stringWithFormat method... Or explain a little more how you do your stuff...

Search through NSArray for string

I would like to search through my NSArray for a certain string.
Example:
NSArray has the objects: "dog", "cat", "fat dog", "thing", "another thing", "heck here's another thing"
I want to search for the word "another" and put the results into one array, and have the other, non results, into another array that can be filtered further.
If the strings inside the array are known to be distinct, you can use sets. NSSet is faster then NSArray on large inputs:
NSArray * inputArray = [NSMutableArray arrayWithObjects:#"one", #"two", #"one again", nil];
NSMutableSet * matches = [NSMutableSet setWithArray:inputArray];
[matches filterUsingPredicate:[NSPredicate predicateWithFormat:#"SELF contains[c] 'one'"]];
NSMutableSet * notmatches = [NSMutableSet setWithArray:inputArray];
[notmatches minusSet:matches];
Not tested so might have a syntax error, but you'll get the idea.
NSArray* inputArray = [NSArray arrayWithObjects:#"dog", #"cat", #"fat dog", #"thing", #"another thing", #"heck here's another thing", nil];
NSMutableArray* containsAnother = [NSMutableArray array];
NSMutableArray* doesntContainAnother = [NSMutableArray array];
for (NSString* item in inputArray)
{
if ([item rangeOfString:#"another"].location != NSNotFound)
[containsAnother addObject:item];
else
[doesntContainAnother addObject:item];
}
It would not work because as per document "indexOfObjectIdenticalTo:" returns the index of the first object that has the same memory address as the object you are passing in.
you need to traverse through your array and compare.

Resources