#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "Person.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"%@", NSHomeDirectory());
NSString *homePath = NSHomeDirectory();
NSLog(@"%@", homePath);
NSString *tmpPath = NSTemporaryDirectory();
NSLog(@"%@", tmpPath);
NSString *preferencePath = [NSHomeDirectory() stringByAppendingString:@"/Library/perferences"];
NSLog(@"%@", preferencePath);
NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Cache"];
NSLog(@"%@", cachePath);
NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@", documents);
NSString *library = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"%@", library);
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"%@", caches);
NSBundle *bundle = [NSBundle mainBundle];
NSLog(@"%@", bundle);
NSString *filePath = [bundle pathForResource:@"" ofType:@""];
NSLog(@"%@", filePath);
NSString *string = @"同志们好! 首长好!";
NSString *stringPath = [NSHomeDirectory() stringByAppendingPathComponent:@"string.txt"];
[string writeToFile:stringPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", [NSString stringWithContentsOfFile:stringPath encoding:NSUTF8StringEncoding error:nil]);
NSArray *array = @[@"星期四", @"星期五"];
NSString *arrayPath = [NSHomeDirectory() stringByAppendingPathComponent:@"array.plist"];
[array writeToFile:arrayPath atomically:YES];
NSLog(@"%@", [NSArray arrayWithContentsOfFile:arrayPath]);
NSDictionary *dic = @{@"name" : @"houlianyuan"};
NSString *dicPath = [NSHomeDirectory() stringByAppendingPathComponent:@"dic.json"];
[dic writeToFile:dicPath atomically:YES];
NSLog(@"%@", [NSDictionary dictionaryWithContentsOfFile:dicPath]);
UIImage *image = [UIImage imageNamed:@"image1.jpg"];
NSData *data = UIImagePNGRepresentation(image);
NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"image1.dat"];
[data writeToFile:dataPath atomically:YES];
UIImage *image2 = [UIImage imageWithData:[NSData dataWithContentsOfFile:dataPath]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(37, 100, 300, 300))];
imageView.image = image2;
[self.window.rootViewController.view addSubview:imageView];
Person *person1 = [[Person alloc] init];
person1.name = @"王右";
person1.age = 16;
person1.image = [UIImage imageNamed:@"image1.jpg"];
NSMutableData *mutableData = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
[archiver encodeObject:person1 forKey:@"person1"];
[archiver finishEncoding];
NSString *archiverPath = [NSHomeDirectory() stringByAppendingPathComponent:@"archiver.dat"];
[mutableData writeToFile:archiverPath atomically:YES];
NSData *data2 = [NSData dataWithContentsOfFile:archiverPath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data2];
Person *person2 = [unarchiver decodeObjectForKey:@"person1"];
[unarchiver finishDecoding];
NSLog(@"%@ %ld", person2.name, person2.age);
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:(CGRectMake(0, 350, 300, 300))];
imageView2.image = person2.image;
[self.window.rootViewController.view addSubview:imageView2];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *grilPath = [NSHomeDirectory() stringByAppendingPathComponent:@"/a/b/grilDic"];
[fileManager createDirectoryAtPath:grilPath withIntermediateDirectories:YES attributes:nil error:nil];
NSString *srcPath = grilPath;
NSString *desPath = [NSHomeDirectory() stringByAppendingPathComponent:@"grilDic"];
[fileManager moveItemAtPath:srcPath toPath:desPath error:nil];
NSString *srcPath2 = desPath;
NSString *desPath2 = [NSHomeDirectory() stringByAppendingPathComponent:@"boyDic"];
[fileManager moveItemAtPath:srcPath2 toPath:desPath2 error:nil];
NSString *srcPath3 = desPath2;
NSString *desPath3 = [NSTemporaryDirectory() stringByAppendingPathComponent:@"boyDic"];
[fileManager copyItemAtPath:srcPath3 toPath:desPath3 error:nil];
NSString *cachePath2 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *imageCache = [cachePath2 stringByAppendingPathComponent:@"imageCache"];
if (![fileManager fileExistsAtPath:imageCache]) {
[fileManager createDirectoryAtPath:imageCache withIntermediateDirectories:YES attributes:nil error:nil];
}
return YES;
}
@end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface Person : NSObject<NSCoding>
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) UIImage *image;
@end
#define KName @"name"
#define KAge @"age"
#define KImage @"image"
#import "Person.h"
@implementation Person
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:KName];
[aCoder encodeInteger:self.age forKey:KAge];
[aCoder encodeDataObject:UIImagePNGRepresentation(self.image)];
}
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:KName];
self.age = [aDecoder decodeIntegerForKey:KAge];
self.image = [UIImage imageWithData:[aDecoder decodeDataObject]];
}
return self;
}
@end