困惑:属性列表在模拟器和其他应用程序中有效,但当我在 iphone 4 上测试时无效

发布于 2024-11-18 08:13:12 字数 2350 浏览 1 评论 0原文

我正在开发一个应用程序,该应用程序将数据从视图写入 .plist,然后将其拉入以填充另一个视图中的一些 UILabels。我过去曾多次这样做过,并且在设备和模拟器上都运行良好。这次它在模拟器上工作正常,但在设备上却不行。我已经查看了所有连接和代码,但找不到它不起作用的原因。我怀疑,由于某种原因,数据不在文件路径的末尾,但如果不是,我无法理解为什么会出现这种情况。

我还尝试通过菜单清理目标并删除并重新安装测试设备上的应用程序,希望这可以纠正这种情况,但没有什么乐趣。

谁能深入了解我为什么会遇到这个问题以及如何纠正它?这就是为什么它可以在模拟器上工作,但不能在手机上工作,这让我感到困惑,特别是当我过去使用相同的代码来制作功能齐全的应用程序时。我正在使用 xCode 4 并在 iphone 4 上进行测试:

代码如下。首先,我检查数据文件路径末尾是否有数据,如下所示:

- (NSString *)dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirctory = [paths objectAtIndex:0];
    return [documentsDirctory stringByAppendingPathComponent:memberDetails];
}

然后在 viewWillAppear 中获取数据并使用它来填充视图中的 UIlabel。 a 一些标签根据其包含的数据而可见或不可见。 viewWillAppear 如下:

-(void)viewWillAppear:(BOOL)animated

{

// Check data file path and if it exists, load data from there. 
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])

    {
        ///All of the following handles the reloading of data from the plist.
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        memName.text = [array objectAtIndex:0];
        memAddress.text = [array objectAtIndex:1];
        memOccupation.text = [array objectAtIndex:2];
        sherex.text = [array objectAtIndex:3];


        if ([[array objectAtIndex:3] isEqualToString:@"winstonwinston"]) {
            cvName.hidden = NO;
            memName.hidden = NO;
            cvAddress.hidden = NO;
            memAddress.hidden = NO;
            cvOccupation.hidden = NO;
            memOccupation.hidden = NO;
            sherex.hidden = YES;
            altText.hidden = YES;


        } 
        else {
            cvName.hidden = YES;
            memName.hidden = YES;
            cvAddress.hidden = YES;
            memAddress.hidden = YES;
            cvOccupation.hidden = YES;
            memOccupation.hidden = YES;
            sherex.hidden = YES;
            altText.hidden = NO;
        }



        [array release];

    }
    [super viewWillAppear:animated];  
    }

也许值得指出的一件事是 if/else 语句似乎也不适用于该设备。换句话说,视图中的所有内容都是可见的。对我来说,这似乎表明问题可能不是数据,因为 if 应该能够确定是否存在名为“winstonwintson”的项目,并相应地显示视图的隐藏/显示元素。

如果有人有一些见解请帮助!我完全被困住了。感谢您花时间阅读本文。

I'm working on an app that writes data out to a .plist from a view and then pulls it in to populate some UILabels in another view. I've done this several times in the past and it has worked fine on both the device and simulator. This time it's working fine on the simulator but not on the device. I've been through looking at all the connections and code but just can't find why it's not functioning. My suspicion is that for some reason the data is not at the end of the file path, but if it isn't I can't understand why this is the case.

I've Also tried cleaning the targets through the menu and deleting and reinstalling the app on the testing device in the hope that this may rectify the situation, but no joy.

Can anyone offer some insight into why I'm having this problem and how to rectify it? It's why it works on the simulator but not the phone that is confusing me, especially when I've used the same code in the past to make fully functioning apps. I'm using xCode 4 and testing on an iphone 4:

The code is as follows. First I check to see if there is data at the end of a data file path like this:

- (NSString *)dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirctory = [paths objectAtIndex:0];
    return [documentsDirctory stringByAppendingPathComponent:memberDetails];
}

Then in viewWillAppear I get the data and use it to populate UIlabels in the View. a Few of the labels are visible or invisible based on the data they contain. The viewWillAppear is as follows:

-(void)viewWillAppear:(BOOL)animated

{

// Check data file path and if it exists, load data from there. 
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])

    {
        ///All of the following handles the reloading of data from the plist.
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        memName.text = [array objectAtIndex:0];
        memAddress.text = [array objectAtIndex:1];
        memOccupation.text = [array objectAtIndex:2];
        sherex.text = [array objectAtIndex:3];


        if ([[array objectAtIndex:3] isEqualToString:@"winstonwinston"]) {
            cvName.hidden = NO;
            memName.hidden = NO;
            cvAddress.hidden = NO;
            memAddress.hidden = NO;
            cvOccupation.hidden = NO;
            memOccupation.hidden = NO;
            sherex.hidden = YES;
            altText.hidden = YES;


        } 
        else {
            cvName.hidden = YES;
            memName.hidden = YES;
            cvAddress.hidden = YES;
            memAddress.hidden = YES;
            cvOccupation.hidden = YES;
            memOccupation.hidden = YES;
            sherex.hidden = YES;
            altText.hidden = NO;
        }



        [array release];

    }
    [super viewWillAppear:animated];  
    }

One thing perhaps worth pointing out is that the if/else statement doesn't seem to be working on the device either. In other words everything in the view is visible. To me this would seem to suggest that the problem might not be the data since if should be able to determine whether or not there is an item called 'winstonwintson' in existence or not and display the hidden/showing elements of the view accordingly.

If anyone has some insight please help! I'm totally stuck. Thanks for taking the time to read this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百变从容 2024-11-25 08:13:12

如果它涉及文件并且可以在模拟器上运行,但不能在设备上运行,则通常是两个问题之一。

  1. 您使用的文件名在不区分大小写的文件系统(模拟器)上可以正常工作,但在设备(区分大小写的文件系统)上则不行。
    我可以想象这对您有何影响的唯一方法是当您尝试将初始数据集复制到文档目录时。例如,如果您尝试从包中复制,请检查 [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"bar"]; 返回的路径,如果路径为零,则您必须再次检查您的文件名,它们必须完全相同。

  2. 您写入设备上不可写的目录。
    一种不太可能的可能性是 memberDetails 包含类似 /../foo 的内容,并且您尝试在不可写的目录中写入。在模拟器上,您可以将文件写入任何您想要的位置,在设备上,此类访问将失败。

因此,您应该尝试回答的两个问题是:

  • 如何在文档目录中保存数据?
  • memberDetails 的内容是什么? (从dataFilePath返回的路径看起来如何?)

编辑:

NSLog对于调试此类问题总是有很大帮助,只需打印出您的路径并检查它们是否正常。

像这样的事情:

NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"bar"];
NSLog(@"Source: %@", sourcePath);
if (![[NSFileManager...

或者您可以使用 if 条件

if (sourcePath == nil) {
    NSLog(@"Something is wrong with the sourcePath, because sourcePath == nil!");
}

编辑2:

因为这些视图配置部分都没有被调用最有可能 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])从来都不是真的。这意味着该文件不存在。

If it involves files and works on the simulator but not on the device it's usually one of two problems.

  1. you use filenames that work ok on a case insensitive file system (the simulator) but not on the device (case sensitive filesystem).
    The only way I can imagine how this affects you is when you try to copy an initial dataset to the documents directory. For example if you try to copy from your bundle, check the path that is returned by [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"bar"]; If the path is nil you have to check your filenames again, they have to be exactly the same.

  2. you write to directories that are not writeable on the device.
    One very unlikely possibility is that memberDetails contains something like /../foo and you try to write in a non writeable directory. On the simulator you can write your files wherever you want, on the device such accesses will fail.

So the two questions you should try to answer are:

  • how do you save the data in the documents directory?
  • what is the content of memberDetails? (how does the returned path from dataFilePath look?)

EDIT:

NSLog is always a great help in debugging such problems, just print out your paths and check if they are ok.

Something like this:

NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"bar"];
NSLog(@"Source: %@", sourcePath);
if (![[NSFileManager...

or you could use a if condition

if (sourcePath == nil) {
    NSLog(@"Something is wrong with the sourcePath, because sourcePath == nil!");
}

EDIT2:

Since neither of those view configuring parts are called most likely if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) is never true. This means the file does not exist.

吹梦到西洲 2024-11-25 08:13:12

从手机中删除应用程序,清理项目,重新构建并运行。有时关闭后台会很有帮助,为此将:添加

<key>UIApplicationExitsOnSuspend</key><true/>

到您的应用程序 plist 中。

Delete app from phone, clean project, re-build and run. It is sometimes helpful to turn off backgrounding, to do that add:

<key>UIApplicationExitsOnSuspend</key><true/>

to your app plist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文