Hi everyone!! I'm going to talk about making Folders in your app.
1) Why would you want to make folders?
A: Folders help keep things organized and can keep data separated. For example, lets say I have students and employees and I'm naming the files after the person's last name. Well if one student is Smith and an employee is named Smith, the file would overwrite, meaning I have the data for either the student or the employee(which ever came second). If I have a employee and student folder, I don't have to worry about that problem occurring.
So Here's what we do:
1)Make a new View based app named Text.
2)In TextViewController.h type this
Code:
#import <UIKit/UIKit.h>
@interface TextViewController : UIViewController
{
IBOutlet UILabel *textLabel;
}
@property (nonatomic, retain) UILabel *textLabel;
-(void)createFolder;
-(void)getText:(NSString *)path;
@end
and save.
3)Go to TextViewController.xib and add a UILabel and connect it. Also make it blank. Save.
4)In TextViewController.m type this:
Code:
#import "TextViewController.h"
@implementation TextViewController
@synthesize textLabel;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
[self createFolder];
[textLabel setText:[self getText:[self getPathToTextFile]]];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc
{
[super dealloc];
[textLabel release];
}
-(void)createFolder
{
NSLog(@"This creating folder");
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *path = [docDir stringByAppendingFormat:@"/text/"];
NSError *error;
if (![manager fileExistsAtPath:path])
{
NSLog(@"Creating");
if (![manager createDirectoryAtPath:path
withIntermediateDirectories:NO
attributes:nil
error:error])
{
NSAssert1(0,@"Failed to make directory with '%s'.", [error localizedDescription]);
}
}
}
-(NSString *)getPathToTextFile
{
NSLog(@"Working");
NSString *string = [[NSString alloc] initWithString:@"This is a nice tutorial"];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [array objectAtIndex:0];
NSString *path = [docDir stringByAppendingFormat:@"/text/text.txt"];
[string writeToFile:path
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
NSLog(path);
return path;
}
-(NSString *)getText:(NSString *)path
{
NSLog(@"Is getting text");
NSString *string = [[NSString alloc] initWithContentsOfFile:path];
return string;
}
@end
and build and run and thats how you do it.
2 important things:
you need to have
Code:
if (![manager fileExistsAtPath:path])
otherwise your app will fail with no console message.
Second if you want to check to see that a folder was made, the NSLog(path); will give you the location.