困惑:属性列表在模拟器和其他应用程序中有效,但当我在 iphone 4 上测试时无效
我正在开发一个应用程序,该应用程序将数据从视图写入 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它涉及文件并且可以在模拟器上运行,但不能在设备上运行,则通常是两个问题之一。
您使用的文件名在不区分大小写的文件系统(模拟器)上可以正常工作,但在设备(区分大小写的文件系统)上则不行。
我可以想象这对您有何影响的唯一方法是当您尝试将初始数据集复制到文档目录时。例如,如果您尝试从包中复制,请检查
[[NSBundle mainBundle] pathForResource:@"foo" ofType:@"bar"];
返回的路径,如果路径为零,则您必须再次检查您的文件名,它们必须完全相同。您写入设备上不可写的目录。
一种不太可能的可能性是 memberDetails 包含类似
/../foo
的内容,并且您尝试在不可写的目录中写入。在模拟器上,您可以将文件写入任何您想要的位置,在设备上,此类访问将失败。因此,您应该尝试回答的两个问题是:
dataFilePath
返回的路径看起来如何?)编辑:
NSLog对于调试此类问题总是有很大帮助,只需打印出您的路径并检查它们是否正常。
像这样的事情:
或者您可以使用
if
条件编辑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.
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.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:
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:
or you could use a
if
conditionEDIT2:
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.从手机中删除应用程序,清理项目,重新构建并运行。有时关闭后台会很有帮助,为此将:添加
到您的应用程序 plist 中。
Delete app from phone, clean project, re-build and run. It is sometimes helpful to turn off backgrounding, to do that add:
to your app plist.