I am making a multi- timer app that uses plist files. the plist files are loaded into a tableview. I don't want the .plist extension from showing in the tableview. Is there a solution for this?
Here is the code that I need help with. this is a NSArray not NSString
self.files = [[NSBundle mainBundle] pathsForResourcesOfType:#"plist" inDirectory:#"Timers1"];
NSString * filenameWithExtension = #"timer1.plist";
NSString * filename = [filenameWithExtension stringByDeletingPathExtension];
returns a string without extension.
You can set the string as table content.
cell.textLabel.text = filename;
The tableview only represents the data, in this case the file name with is a NSString. Before you set the cells contents just rename the string
NSString *filenameWithExtension = #"timer1.plist";
NSString *filename = [filenameWithExtension stringByReplacingOccuranceOfString:#".plist" withString:#""];
cell.textLabel.text = filename;
There are other String/Path helpers that can help you achieve the desired effect too.
Related
I'm going to ask this question again,I thought I had figured it out but I guess not. This is for a timer app that gets the timer info from a plist file. Everything works but I want the .plist extension not to show in my tableview. Here are two code snippets that I believe are the issue. These are in an array called NSArray *files;
self.files = [[NSBundle mainBundle] pathsForResourcesOfType:#"plist" inDirectory:#"Timers1"];
// Configure the cell.
cell.textLabel.text = [[files objectAtIndex:indexPath.row] lastPathComponent];
return cell;
Thanks for the help
NSString *text = [[files objectAtIndex:indexPath.row] lastPathComponent];
text = [text substringToIndex:[text rangeOfString:#"."].location];
cell.textLabel.text = text;
I am developing an application where i am creating some random .txt file which ultimately stored as a txt file format.
Now for reading these file i am using this code
NSString* documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSError* error = nil;
myArray = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectory error:&error]retain];
Now i am displaying the file name in a table view using
cell.textLabel.text = [myArray objectAtIndex:row];
So it curretly displaying like file1.txt,file2.txt etc.
But i donot want the file extension while displaying in the table list.
How can i do that?
cell.textLabel.text = [[myArray objectAtIndex:row] stringByDeletingPathExtension];
Use [yourString stringByDeletingPathExtension] method to remove the extensions from your string. Refer NSString class.
I would like to know how to selectively trim an NSMutableString. For example, if my string is "MobileSafari_2011-09-10-155814_Jareds-iPhone.plist", how would I programatically trim off everything except the word "MobileSafari"?
Note : Given the term programatically above, I expect the solution to work even if the word "MobileSafari" is changed to "Youtube" for example, or the word "Jared's-iPhone" is changed to "Angela's-iPhone".
Any help is very much appreciated!
Given that you always need to extract the character upto the first underscore; use the following method;
NSArray *stringParts = [yourString componentsSeparatedByString:#"_"];
The first object in the array would be the extracted part you need I would think.
TESTED CODE: 100% WORKS
NSString *inputString=#"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";
NSArray *array= [inputString componentsSeparatedByString:#"_"];
if ([array count]>0) {
NSString *resultedString=[array objectAtIndex:0];
NSLog(#" resultedString IS - %#",resultedString);
}
OUTPUT:
resultedString IS - MobileSafari
If you know the format of the string is always like that, it can be easy.
Just use NSString's componentsSeparatedByString: documented here.
In your case you could do this:
NSString *source = #"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";
NSArray *seperatedSubStrings = [source componentsSeparatedByString:#"_"];
NSString *result = [seperatedSubStrings objectAtIndex:0];
#"MobileSafari" would be at index 0, #"2011-09-10-155814" at index 1, and #"Jareds-iPhone.plist" and at index 2.
Try this :
NSString *strComplete = #"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";
NSArray *arr = [strComplete componentsSeparatedByString:#"_"];
NSString *str1 = [arr objectAtIndex:0];
NSString *str2 = [arr objectAtIndex:1];
NSString *str3 = [arr objectAtIndex:2];
str1 is the required string.
Even if you change MobileSafari to youtube it will work.
So you'll need an NSString variable that'll hold the beginning of the string you want to truncate. After that one way could be to change the string and the variable string values at the simultanously. Say, teh Variable string was "Youtube" not it is changed to "MobileSafari" then the mutable string string should change from "MobileSafari_....." to "YouTube_......". And then you can get the variable strings length and used the following code to truncate the the mutable string.
NSString *beginningOfTheStr;
.....
theMutableStr=[theMutableStr substringToIndex:[beginningOfTheStrlength-1]];
See if tis works for you.
I want to populate one.plist files from a number of .plist files as per the user selection .
How to populate UIPickerView with a .plist file ?
I don't understand your question well but here is what I guess. You may want to load data from a .plist file and then fill in the UIPickerView.
Here is a sample code for loading a .plist file:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"money.plist"];
NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
In my application one is mainviewcontroller which is subclass of uiTableviewcontroller holds data from plist when clicked on a particular cell its displays detailviewcontroller(dvc) which is subclass of uiviewcontroller. dvc holds textfield & button .actually when i clicked on button i write textfield's text on tableviewcell through plist but it is actually write data temporary but i want to write data permanently on tableview what can i do for that .its a navigation based application?
Try this:
Loading the plist to dictionary:
//Get the path to documents directory
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:#"listData.plist"];
//if the file does not exist in documents directory, then load from the bundle.
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:#"listData" ofType:#"plist"];
}
//load plist into NSMutableDictionary.
nes.plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
Saving dictionary to plist:
//Get the path to documents directory
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:#"listData.plist"];
//Write to documents directory! NOT bundle!!
[nes.plistDict writeToFile:plistPath atomically: YES];
Also use:
[nes.plistDict setObject:txtName.text forKey:#"col1"];
Not:
[nes.plistDict setValue:txtName.text forKey:#"col1"];
If this does not help can I see youre cellForRowAtIndexPath in mainviewcontroller?
could you show us some code?
What is the code for the action button? How do you reload the table view?
I'm guessing you are reading a plist into a NSDictionary? Then loading the tableviews cell.text from the values in the dictionary? When you push the button do you write the textfield text to the NSDIctionary? Do you remember to write the NSDictionary back to the plist?
Okay.
How do you load youre tableview cells?
Are you sure the value for col1 is of NSString type?
You should probably use setObject: forKey: instead of SetValue: forKey:..
Does the tableview not update at all or is it just that it is not saved after you quit?
Are you testing on simulator or device?
The application bundle is a readonly directory on device but not in simulator. you should use:
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
NSString *plistPath = [rootPath
stringByAppendingPathComponent:#"listData.plist"];