How to store last part of string? - ios4

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

Related

Xcode - read a file containing NSDate objects

Every time an event is triggered, my app records its date:
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = [path stringByAppendingPathComponent:#"dates.dat"];
if (![[NSFileManager defaultManager] fileExistsAtPath:filename]) {
[[NSFileManager defaultManager] createFileAtPath:filename
contents:nil
attributes:nil];
}
NSFileHandle *wHandle = [NSFileHandle fileHandleForWritingAtPath:filename];
[wHandle seekToEndOfFile];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[NSDate date]];
[wHandle writeData:data];
[wHandle closeFile];
I successfully record the events' dates. But now I am having troubles in reading them back. I tried this but the app crashes:
NSData *restore = [NSData dataWithContentsOfFile:filename];
NSArray *date1 = [NSKeyedUnarchiver unarchiveObjectWithData:restore]; // crash here!
I notice that it takes 223 bytes for each NSDate write but that is not officially mentioned. So I am afraid that using 223 bytes as length to parse the file "dates.dat" would cause problems later.
Do you have any ideas to read dates.dat into an NSArray so I can proceed its values?
Thanks in advance
Don't store the dates as arbitrary blocks of data in a file (because you're correct, you shouldn't rely on the number of bytes used for each date).
As a minimum, use a separator character (like a carriage return) so you know which data belongs to each different date. Then you need to parse the file and read in only the appropriate data before you try to unarchive it.

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

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];
}

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.

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...

Resources