Storing and reading files from Documents directory iOS 5

泪湿孤枕 提交于 2019-12-21 20:12:32

问题


In my game, when a level is completed the app stores a "1" in a file in the Documents directory of the app. When the game then loads, a player can only play a level if the previous level has been completed. When I test the game via Xcode and on a device the app works properly and a level cannot be played until the previous level has been completed. However, when the app was approved and released on the App Store, the app behaves as if each level has been completed (no locked levels). I can't figure this one out and would appreciate someone's help! The devices I'm testing on are all iOs 5.0 or higher.

Below is the code that saves the completed level in the Documents directory:

 NSMutableData* data = [[NSMutableData alloc] init];
        NSKeyedArchiver* coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        NSString *levelString = [NSString stringWithFormat:@"Level%d",level];
        [coder encodeInteger:1 forKey:levelString];
        [coder finishEncoding];
        NSString *levelString2 = [NSString stringWithFormat:@"Level%d.save",level];
        ///
        NSFileManager *filemgr;
        NSString *dataFile;
        NSString *docsDir;
        NSArray *dirPaths;

        filemgr = [NSFileManager defaultManager];

        // Identify the documents directory
        dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        docsDir = [dirPaths objectAtIndex:0];

        // Build the path to the data file
        dataFile = [docsDir stringByAppendingPathComponent:levelString2];

        // Check if the file already exists
        if ([filemgr fileExistsAtPath: dataFile])
        {
            [[NSFileManager defaultManager] removeItemAtPath:dataFile error:nil];
        }
        [data writeToFile:dataFile atomically:YES];
        [coder release];
        [data release];
    }
    @catch (NSException* ex) 
    {
        CCLOG(@"level save failed: %@", ex);
    }

Below is the code that reads the Document directory to see if the level has been completed:

if ([self loadCompletedLevels:6] == 1) { //// level gets unlocked **** }

-(int) loadCompletedLevels:(int)theLevel; {

int isLevelCompleted;  //1 = completed

NSString* kSaveFile = [NSString stringWithFormat:@"Level%d.save",theLevel];
NSString *levelString = [NSString stringWithFormat:@"Level%d",theLevel];

@try
{

    NSFileManager *filemgr;
    NSString *dataFile;
    NSString *docsDir;
    NSArray *dirPaths;

    filemgr = [NSFileManager defaultManager];

    // Identify the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the data file
    dataFile = [docsDir stringByAppendingPathComponent:kSaveFile];


    if ([[NSFileManager defaultManager] fileExistsAtPath:dataFile])
    {
        NSData* data = [[NSData alloc] initWithContentsOfFile:dataFile];

        if (data && [data length] > 0)
        {
            NSKeyedUnarchiver* decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
            isLevelCompleted = [decoder decodeIntForKey:levelString];

            [decoder release];
        }

        [data release];
    }
    if (isLevelCompleted == 1) {
        levelCompleted = YES;

    }
}
@catch (NSException* ex) 
{

    levelCompleted = NO;
}
return isLevelCompleted; }

回答1:


You should probably use a different approach for storing your data, but the real problem is that you are not initializing the return value, isLevelCompleted. It is on the stack, and does not have a default value. It starts out with whatever happens to be at that stack location.

So, if you don't set it, it will have an arbitrary value.

Also, you should probably use BOOL for a boolean value, but if you make this:

int isLevelCompleted = 0;  //1 = completed

you will initialize it to "false" so it must be explicitly changed to "true" by your code.



来源:https://stackoverflow.com/questions/10201944/storing-and-reading-files-from-documents-directory-ios-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!