File saved outside of documents directory


#1

For some reason my archive is saved outside the document directory eventhough the codes are the same. Anyone help pls?


#2

Can we see your code?


#3

BNRItemStore.h

[code]#import <Foundation/Foundation.h>

@class BNRItem;

@interface BNRItemStore : NSObject

@property (nonatomic, readonly) NSArray *allItems;

// Notice that this is a class method and prefixed with a + instead of a -

  • (instancetype)sharedStore;
  • (BNRItem *)createItem;

  • (void)removeItem:(BNRItem *)item;

  • (void)moveItemAtIndex:(NSInteger)fromIndex
    toIndex:(NSInteger)toIndex;
    -(BOOL)saveChanges;

@end
[/code]

BNRItemStore.m

[code]#import “BNRItemStore.h”
#import “BNRItem.h”
#import “BNRImageStore.h”

@interface BNRItemStore ()

@property (nonatomic) NSMutableArray *privateItems;

@end

@implementation BNRItemStore

-(BOOL)saveChanges
{
NSString *path = [self itemArchivePath];
//Return YES on success
return [NSKeyedArchiver archiveRootObject:self.privateItems toFile:path];

}

-(NSString *)itemArchivePath
{
//Make sure that the first argument is NSDocumentDirectory
//and not NSDocumentationDirectory
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//Get the one document directory from that list
NSString *documentDirectory = [documentDirectories firstObject];
return [documentDirectory stringByAppendingString:@“items.archive”];
}

  • (instancetype)sharedStore
    {
    static BNRItemStore *sharedStore;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{sharedStore = [[self alloc]initPrivate];});
    return sharedStore;
    }

// If a programmer calls [[BNRItemStore alloc] init], let him
// know the error of his ways

  • (instancetype)init
    {
    @throw [NSException exceptionWithName:@"Singleton"
    reason:@"Use +[BNRItemStore sharedStore]"
    userInfo:nil];
    return nil;
    }

// Here is the real (secret) initializer

  • (instancetype)initPrivate
    {
    self = [super init];
    if (self) {
    _privateItems = [[NSMutableArray alloc] init];
    }
    return self;
    }

  • (NSArray *)allItems
    {
    return [self.privateItems copy];
    }

  • (BNRItem *)createItem
    {
    BNRItem *item = [BNRItem randomItem];

    [self.privateItems addObject:item];

    return item;
    }

  • (void)removeItem:(BNRItem *)item
    {
    NSString *key = item.itemKey;
    if (key) {
    [[BNRImageStore sharedStore] deleteImageForKey:key];
    }

    [self.privateItems removeObjectIdenticalTo:item];
    }

  • (void)moveItemAtIndex:(NSInteger)fromIndex
    toIndex:(NSInteger)toIndex
    {
    if (fromIndex == toIndex) {
    return;
    }
    // Get pointer to object being moved so you can re-insert it
    BNRItem *item = self.privateItems[fromIndex];

    // Remove item from array
    [self.privateItems removeObjectAtIndex:fromIndex];

    // Insert item in array at new location
    [self.privateItems insertObject:item atIndex:toIndex];
    }

@end
[/code]

BNRItem.h

#import <Foundation/Foundation.h>

@interface BNRItem : NSObject<NSCoding>

+ (instancetype)randomItem;

- (instancetype)initWithItemName:(NSString *)name
                  valueInDollars:(int)value
                    serialNumber:(NSString *)sNumber;
 
@property (nonatomic, copy) NSString *itemName;
@property (nonatomic, copy) NSString *serialNumber;
@property (nonatomic) int valueInDollars;
@property (nonatomic, readonly, strong) NSDate *dateCreated;

@property (nonatomic, copy) NSString *itemKey;

@end

BNRItem.m

#import "BNRItem.h"

@interface BNRItem ()

@property (nonatomic, strong) NSDate *dateCreated;

@end

@implementation BNRItem

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.itemName forKey:@"itemName"];
    [aCoder encodeObject:self.serialNumber forKey:@"serialNumber"];
    [aCoder encodeObject:self.dateCreated forKey:@"dateCreated"];
    [aCoder encodeObject:self.itemKey forKey:@"itemKey"];
    [aCoder encodeInt:self.valueInDollars forKey:@"valueInDollars"];
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        _itemName = [aDecoder decodeObjectForKey:@"itemName"];
        _serialNumber = [aDecoder decodeObjectForKey:@"serialNumber"];
        _dateCreated = [aDecoder decodeObjectForKey:@"dateCreated"];
        _itemKey = [aDecoder decodeObjectForKey:@"itemKey"];
        _valueInDollars = [aDecoder decodeIntForKey:@"valueInDollars"];
    }
    return self;
}



+ (id)randomItem
{
    // Create an array of three adjectives
    NSArray *randomAdjectiveList = @[@"Fluffy", @"Rusty", @"Shiny"];
    // Create an array of three nouns
    NSArray *randomNounList = @[@"Bear", @"Spork", @"Mac"];
    // Get the index of a random adjective/noun from the lists
    // Note: The % operator, called the modulo operator, gives
    // you the remainder. So adjectiveIndex is a random number
    // from 0 to 2 inclusive.
    NSInteger adjectiveIndex = rand() % [randomAdjectiveList count];
    NSInteger nounIndex = rand() % [randomNounList count];
    // Note that NSInteger is not an object, but a type definition
    // for "unsigned long"
    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
                                randomAdjectiveList[adjectiveIndex],
                                randomNounList[nounIndex]];
    int randomValue = rand() % 100;
    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                        '0' + rand() % 10,
                                        'A' + rand() % 26,
                                        '0' + rand() % 10,
                                        'A' + rand() % 26,
                                        '0' + rand() % 10];
    BNRItem *newItem =
        [[self alloc] initWithItemName:randomName
                        valueInDollars:randomValue
                          serialNumber:randomSerialNumber];
    return newItem;
}

- (id)initWithItemName:(NSString *)name
        valueInDollars:(int)value
          serialNumber:(NSString *)sNumber
{
    // Call the superclass's designated initializer
    self = [super init];
    // Did the superclass's designated initializer succeed?
    if (self) {
        // Give the instance variables initial values
        self.itemName = name;
        self.serialNumber = sNumber;
        self.valueInDollars = value;
        self.dateCreated = [[NSDate alloc] init];

        // Create a NSUUID object - and get its string representation
        NSUUID *uuid = [[NSUUID alloc] init];
        NSString *key = [uuid UUIDString];
        _itemKey = key;
    }
    
    // Return the address of the newly initialized object
    return self;
}

- (id)init {
    return [self initWithItemName:@"Item"
                   valueInDollars:0
                     serialNumber:@""];
}

- (NSString *)description
{
    NSString *descriptionString =
        [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
                            self.itemName,
                            self.serialNumber,
                            self.valueInDollars,
                            self.dateCreated];
    return descriptionString;
}

- (void)dealloc
{
    NSLog(@"Destroyed: %@", self);
}

@end

BNRAppDelegate.m

[code]- (void)applicationDidEnterBackground:(UIApplication *)application
{
BOOL success = [[BNRItemStore sharedStore]saveChanges];
if (success) {
NSLog(@“Saved all of the BNRItems”);
}else{
NSLog(@“Could not save any of the BNRItems”);
}

}
[/code]


#4

Ah, in the BNRItemStore, you should be doing:

[documentDirectory stringByAppendingPathComponent:@"items.archive"]

#5

Can’t believe i missed that. Thanks a lot.