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.
Related
In my application I want to implement a simple Alarm function. I know how to use UILocalNotifications, but I came across this source code with a like UI of the iPhone's native Clock app alarm area as well as it having a believe a type of data persistence. Two things I am not good at interface design and data persistence this source code has. But I downloaded it and started playing around with it to find the alarms are not persistent.
Download
Does anyone know how the source code can be adjusted so that it is persistent and the plist can be saved and read to and from? I am open to learning too, this area is somewhat unknown to me too. Thanks
I review your code and find issue that you not moved your "Alarms.plist" file form resource to document directory. we are not able to edit file which is in resource folder. so write following code in app delegate file.
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *theFileName = #"Alarms.plist"; //Change this appropriately
NSString *oldPath = [[NSBundle mainBundle] pathForResource:#"Alarms" ofType:#"plist"];//[NSString stringWithFormat:#"%#/Inbox/%#", documentsDirectory, theFileName];
NSString *newPath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, theFileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:newPath])
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:nil];
Perform save operation on file which is in Document directory folder.
try this code... to save plist from bundle to Document Directory
Notice that you will have "Unable to read... " just at the first app launch
- (NSMutableArray *)displayedObjects
{
if (_displayedObjects == nil)
{
NSString *path = [[self class] pathForDocumentWithName:#"Alarms.plist"];
NSArray *alarmDicts = [NSMutableArray arrayWithContentsOfFile:path];
if (alarmDicts == nil)
{
NSLog(#"Unable to read plist file: %#", path);
NSLog(#"copy Alarms.plist to: %#", path);
NSString *pathToSetingbundle = [[NSBundle mainBundle] pathForResource:#"Alarms" ofType:#"plist"];
[[NSFileManager defaultManager]copyItemAtPath:pathToSetingbundle toPath:path error:nil];
}
_displayedObjects = [[NSMutableArray alloc]
initWithCapacity:[alarmDicts count]];
for (NSDictionary *currDict in alarmDicts)
{
Alarm *alarm = [[Alarm alloc] initWithDictionary:currDict];
[_displayedObjects addObject:alarm];
NSLog(#"#disply obj %#", alarm);
}
}
return _displayedObjects;
}
I've been trying to save to a property list file, however it's not working as its only saving the object to the array not the actual file itself, meaning that what it saves isn't obviously persisting.
[[category objectAtIndex:questionCounter] replaceObjectAtIndex:5 withObject:myString];
[category writeToFile:filePath atomically:YES];
I'm using this earlier on to save the property list files to the document directory so that I can edit and save to them:
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistFilePath = [documentsDirectory stringByAppendingPathComponent:fileNameFull];
if([[NSFileManager defaultManager] fileExistsAtPath:plistFilePath]) {
category = [NSMutableArray arrayWithContentsOfFile:plistFilePath];
NSLog(#"Files exist.");
}
else {
filePath = [[NSBundle mainBundle] pathForResource:fileNamer ofType:#"plist"];
category = [NSMutableArray arrayWithContentsOfFile:filePath];
NSLog(#"Files have been created.");
}
My property list is made up of arrays, within those arrays I'm trying to save my object (the string). I have a feeling it's something trivial but I can't spot it.
When you are trying to save your array, you are passing in filePath which points to the main bundle directory, which you can't write to. You want to use plistFilePath instead, like so:
[category writeToFile:plistFilePath atomically:YES];
I have integrated dropbox in my app. I have it up to the point where it displays the files to the user and they can select the ones to download. I know I need to call this line but I don't know what the local file path on an iPhone is. It only needs to be temporary because once I have the text file I will process it... My question is what is the local file path. Thank You in advance.
[[self restClient] loadFile:[filePaths objectAtIndex:indexPath.row] intoPath:localPath]
UPDATED BASED ON ANSWER BELLOW:::STILL NOT WORKING
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *localPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:[filePaths objectAtIndex:indexPath.row]];
[[self restClient] loadFile:[filePaths objectAtIndex:indexPath.row] intoPath:localPath];
NSLog(#"%#",[NSString stringWithContentsOfFile:localPath encoding:NSUTF8StringEncoding error:nil]);
}
you usually just use your local document directory.
Try this:
NSString *localPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:[filePaths objectAtIndex:indexPath.row]];
You're probably looking for:
NSString * tempPath = [NSSearchPathForDirectoriesInDomains(NSTemporaryDirectory(), NSUserDomainMask, YES) objectAtIndex:0];
NSString * yourFile = [filePaths objectAtIndex:indexPath.row];
if(yourFile != nil) {
NSString * filePath = [tempPath stringByAppendingPathComponent:yourFile];
}
else {
// Check your file
}
For historic reasons the need for a temp directory existed in Unix long before Objective-C. So the way to get the temp directory in our language is crafted on top of an existing Unix method using confstr. It’s also in NSPathUtilities for your convenience.
NSString *tmpDirectory = NSTemporaryDirectory();
NSString *tmpFile = [tmpDirectory stringByAppendingPathComponent:#"temp.txt"];
My NSArray is:
self.files = [bundle pathsForResourcesOfType:#"ppt" inDirectory:#"thepowerpoints"];
This returns the full path of
/User/Name/.../filename.ppt
I can use a NSString to get only the filename using:
NSString *documentsDirectoryPath = [self.files objectAtIndex:thepath.row];
self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension];
Is there a way that I can cleanup the original NSArray to only return the lastPathComponent without the extension?
Am needing to do this to search within the tableview array. Currently, if a file is named "Test.ppt" the search bar will show it if I type in "/User/Name", because the Array it is searching includes the entire path.
I would prefer for it to only search the filename.
NSMutableArray *names = [NSMutableArray arrayWithCapacity:[self.files count]];
for (NSString *path in self.files) {
[names addObject:[[path lastPathComponent] stringByDeletingPathExtension]];
}
self.files = names;
edited.
Hey, I am trying to write an NSMutableArray to a plist.
The compiler does not show any errors, but it does not write to the plist anyway.
I have tried this on a real device too, not just the Simulator.
Basically, what this code does, is that when you click the accessoryView of a UITableViewCell, it gets the indexPath pressed, edits an NSMutableArray and tries to write that NSMutableArray to a plist. It then reloads the arrays mentioned (from multiple plists) and reloads the data in a UITableView from the arrays.
Code:
NSIndexPath *indexPath = [table indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:table]];
[arrayFav removeObjectAtIndex:[arrayFav indexOfObject:[NSNumber numberWithInt:[[arraySub objectAtIndex:indexPath.row] intValue]]]];
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:#"arrayFav.plist"];
NSLog(#"%# - %#", rootPath, plistPath);
[arrayFav writeToFile:plistPath atomically:YES];
// Reloads data into the arrays
[self loadDataFromPlists];
// Reloads data in tableView from arrays
[tableFarts reloadData];
CFShow() on the array after removing one of them shows this:
<CFArray 0x6262110 [0x2c810a0]>{type = mutable-small, count = 4, values = (
0 : <CFNumber 0x6502e10 [0x2c810a0]>{value = +3, type = kCFNumberSInt32Type}
1 : <CFNumber 0x6239de0 [0x2c810a0]>{value = +8, type = kCFNumberSInt32Type}
2 : <CFNumber 0x6239dc0 [0x2c810a0]>{value = +10, type = kCFNumberSInt32Type}
3 : <CFNumber 0x6261420 [0x2c810a0]>{value = +40, type = kCFNumberSInt64Type}
DEBUG-INFO: writeToPlist shows YES, I have tried to release all the arrays before filling them up again, setting them to nil, set atomically to NO.
As discussed in the comments below, the actual problem here is that the plist is being read from and written to two different locations. Somewhere in the app, there is code that reads the file into the array similar to this:
NSString *plistFavPath = [[NSBundle mainBundle] pathForResource:#"arrayFav"
ofType:#"plist"];
arrayFav = [[NSMutableArray alloc] initWithContentsOfFile:plistFavPath];
This logic reads the array from the application's bundle, which is a read-only location and part of the distributed app. Later when the edited array is persisted, code similar to this is used:
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
NSString *plistPath = [rootPath
stringByAppendingPathComponent:#"arrayFav.plist"];
NSLog(#"%# - %#", rootPath, plistPath);
[arrayFav writeToFile:plistPath atomically:YES];
The result here is that the updated file gets written to the app's documents directory, but it is never read from there, giving the appearance that the file is not being saved correctly. To correct this, you should change the code that reads the file to use the same path that you are writing to.
If you need to distribute a default version of the plist for use on the initial launch before the array has been edited, you could continue to include a version of the file in your bundle and then add code to your app delegate that check if the file exists in the documents directory and if it is not present, copies the bundle's default version of the file to the proper place.
[yourMutableArray writeToFile:fileName atomically:YES];
This should work. NSMutableArray inherits from NSArray which has a method to write to a plist.
writeToFile:atomically: won't work if your array contains custom objects.
If your array contains custom objects that are not Plist objects (NSArray, NSDictionary, NSString, NSNumber, etc), then you will not be able to use this method. This method only works on Plist objects.
Another option would be to use the NSCoding protocol, and write your objects to disk that way.
Yes
Look at the Property List Programming Guide.
phoneNumbers is a NSMutableArray
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
NSString *error;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:#"Data.plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: personName, phoneNumbers, nil]
forKeys:[NSArray arrayWithObjects: #"Name", #"Phones", nil]];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if(plistData) {
[plistData writeToFile:plistPath atomically:YES];
}
else {
NSLog(error);
[error release];
}
return NSTerminateNow;
}