iOS pathForResource show file name only - iphone

I am implementing an audio player in my app. and I have a UITableView in which i show my list of audio tracks.
the list of audio is added into an array with
fileArray = [[NSMutableArray alloc] initWithObjects:
[[NSBundle mainBundle] pathForResource:#"A Million Years" ofType:#"mp3"],
[[NSBundle mainBundle] pathForResource:#"2 Million Years" ofType:#"mp3"],...
and in my cellForRowAtIndexPath I am doing
cell.textLabel.text = [[NSString stringWithFormat:#"%#", [self.fileArray objectAtIndex:indexPath.row]] stringByDeletingPathExtension];
but the problem is that it still shows me an entire extension "/Users/....". i only want the file name ("A Million Years", "2 Million Years") to be shown.
What am I doing wrong? I have been looking around for an answer but was never able to get it (perhaps due to wrong phrasing)
Thanks very much everybody. :)

You should use lastPathComponent if you want the last part of it. So:
cell.textLabel.text = [[[self.fileArray objectAtIndex:indexPath.row] lastPathComponent] stringByDeletingPathExtension];

That is not the extension. It's called the 'path'. (It would be good to at least get yourself to an acceptable level of knowledge in using a computing device before trying to program it...)
I suggest you use
[somePath lastPathComponent]
instead in order to get the filename only without the containing directories.
Also, the call to [NSString stringWithFormat:] is completely superfluous, wastes memory and processor time (which is unacceptable, especially on an embedded device).

Related

Basics Introduction To Using CHCSVParser

I'm implementing CHCSVParser into my iPhone app (thanks Dave!) however I'm really confused on how to use it. I've read the read-me and searched some questions on SO but still not 100% sure what to do.
I have a .CSV file with maybe 5000 rows of data and 3-4 columns.
I want this data to in return, load my UITableView along with its corresponding detailViewController.
So I'm assuming I need to somehow implement the API's array method but can anyone help get me started?
I'm glad you like it :)
Basically, CHCSVParser only parses CSV files. You give it a path to a CSV file, and it'll give you back a whole bunch of NSStrings. What you do beyond that point is entirely up to you.
So let's say you've included a CSV file in your iOS app called "Data.csv". Here's how you'd use CHCSVParser to parse it:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"csv"];
NSError *error = nil;
NSArray *rows = [NSArray arrayWithContentsOfCSVFile:path encoding:NSUTF8StringEncoding error:&error];
if (rows == nil) {
//something went wrong; log the error and exit
NSLog(#"error parsing file: %#", error);
return;
}
At this point, rows is an array. Each element in rows is itself an array representing a single row in the CSV file. And each element of that array is an NSString.
So let's say your CSV file looks like this:
Barringer,Arizona,United States,Earth
"Chicxulub, Extinction Event Crater",,Mexico,Earth
Tycho,,,Moon
Lonar,Maharashtra,India,Earth
If you run it through the parser, you'll get back the equivalent of this:
[NSArray arrayWithObjects:
[NSArray arrayWithObjects:#"Barringer",#"Arizona",#"United States",#"Earth",nil],
[NSArray arrayWithObjects:#"Chicxulub, Extinction Event Crater",#"",#"Mexico",#"Earth",nil],
[NSArray arrayWithObjects:#"Tycho",#"",#"",#"Moon",nil],
[NSArray arrayWithObjects:#"Lonar",#"Maharashtra",#"India",#"Earth",nil],
nil];
What you do with it then is your business. The CSV parser doesn't know anything about UITableView, so you get to take this data and re-structure it in a way that you're comfortable dealing with and that fits in to your data model.
Also, remember that by using CHCSVParser, you agree to abide the terms under which it is made available. :)

using finch by zoul, is loading 12MB of wavs into an array causing an erratic crash for my users at launch?

So my code works for about 90% of people. The rest get a crash at launch. I figure with all the memory I'm tying up here, this is the problem?
Maybe i shouldn't use an array as such?
for(i=60;i<80;i++){
myNewString = [NSMutableString stringWithFormat:#"%i", i ];
if (finchKeys[i]==nil)
finchKeys[i] = [[Sound alloc] initWithFile:
[[NSBundle mainBundle] URLForResource:myNewString withExtension:#"wav"]];
}
Is there something I can call to free up memory before I load my wavs?
thanks!!
I imagine you're not playing all of the wavs at once. Why not just save the wav url in your Sound class instead of the whole wav? Then you can just load each wav when you need to play it.

NSKeyedUnarchiver: iPhone .v. Simulator

I'm doing:
NSString *current_path = [[NSBundle mainBundle] bundlePath];
NSString *string_path = [NSString stringWithFormat:
#"%#/filedstring", current_path];
my_string_ = [[NSKeyedUnarchiver unarchiveObjectWithFile:string_path] retain];
The archived string is the text from a UITextField which we unarchive here. I've tried with and without current_path.
This all works fine when running in simulator (class member NSString *my_string_ is not nil) but when run on my iPhone my_string_ is nil.
Why is that?
Thanks for the quick responses all.
Adding to Jason Coco's answer, archive to and unarchive here:
NSString *library_path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
NSString *username_path = [library_path
stringByAppendingPathComponent:#"Caches/filedstring"];
You can't write into the main bundle on the phone, it's not allowed. That's why you don't find your archive there later. The simulator, since it actually runs on Mac OS X doesn't work this way, so it will actually write the file.
If you need to write something, you have to use one of the writeable paths available to your application. For more information, see the iOS Application Programming Guide / The File System. If you're going to do iOS Application Development, you should definitely read and understand this entire document.
As #middaparka says, there is probably something wrong with that file. Here's how I would debug this problem:
First step,
[[NSFileManager defaultManager] fileExistsAtPath: string_path];
Second step,
NSError *err;
NSString *tmp = [NSString stringWithContentsOfFile:string_path encoding:NSUTF8StringEncoding error:&err];
NSLog(#"Contents of string %#",tmp);
Once you've done those, you should have a much clearer idea why your NSKeyedUnarchiver is failing.
Also, check out NSString's stringByAppendingPathComponent: method.

Problem with reading data from plist in the iPhone SDK

I'm creating a myDb.plist file in my resources folder and trying to read it, but it's not getting read. I'm using the following code.
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"myDb" ofType:#"plist"];
contentArray = [NSArray arrayWithContentsOfFile:plistPath];
contentArray is showing null.
What can I try to resolve this?
Make your plist data array and not dictionary. Dictionary also works but i tried with hit and trial. Also, print NSLog your data, if in case you need, to check the input from plist.

read single line from text file in objective-C

i'm new to iPhone programming and coding in XCode SDK.I want to access and read the configuration file which i have placed in Resource folder in XCode,my configuration file looks like this
#key=value$
#vinu=flower$
#cathy=fruit$
I want to compare the key and access the value from configuration file.
Since i'm working with iPhone OS, i cant use NSWorkspace.Hence i'm using NSFileHandle.
This is how my code looks like,
NSString *path = [[NSBundle mainBundle] pathForResource:#"configuration" ofType:#"txt"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *txtString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
please let me know is the procedure correct, and how to proceed. ??
Thank You.
Do yourself a favor and save it as a plist, and not a straight text file.
However, if you need to read it in like that, the simplest way is to read it into a string and then go from there, ie:
NSString * fileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
If the file is super large and the only reasonable way to read it is line-by-line, then you can quickly see that this is something that has come up before (Particularly: Objective-C: Reading a file line by line).