从文档目录iOS 5中存储和读取文件 [英] Storing and reading files from Documents directory iOS 5

查看:112
本文介绍了从文档目录iOS 5中存储和读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的游戏中,当一个关卡完成后,应用程序会在应用程序的Documents目录中的文件中存储一个1。当游戏然后加载时,如果先前的级别已经完成,则玩家只能玩级别。当我通过Xcode测试游戏和在设备上的应用程序正常工作,一个级别无法播放,直到上一级完成。但是,当应用程序在App Store上获得批准并发布时,该应用程序的行为就好像每个级别都已完成(没有锁定级别)。我不能算出这一个,并希望有人的帮助!

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.

以下是将完成的级别保存在Documents目录中的代码:

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);
    }

下面是读取Document目录以查看级别是否完成:

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; }


推荐答案

数据,但真正的问题是,你不是初始化返回值,isLevelCompleted。它在堆栈上,没有默认值。

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.

此外,你应该使用BOOL作为布尔值,但如果你这样做:

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

int isLevelCompleted = 0;  //1 = completed

您将初始化为false,因此必须明确更改为 true。

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

这篇关于从文档目录iOS 5中存储和读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆