运行 addToFavourites 函数时的 EXC_BAD_ACCESS
我有一个 addToFavourites 函数,它导致 EXC_BAD_ACCESS 错误。 基本上,我有收藏夹视图控制器,其中包含用户之前添加的所有收藏夹的列表,现在当单击其中一个收藏夹时,它会将视图推送到详细视图控制器,这工作正常。在详细视图控制器上,我有一个“添加到收藏夹”按钮,如果检测到该键已存在于 addToFavourites 字典中,则该按钮将更改为从收藏夹中删除。当用户从收藏夹页面以及列出所有detailViews的主页访问此detailViewController时,就会出现问题。他们能够做到这一点,因为我有一个标签栏。
所以说我在通过收藏夹页面访问的详细视图中,收藏夹按钮有“从收藏夹中删除”,这是正确的。但如果我单击“从收藏夹中删除”按钮,该按钮会更改为“添加到收藏夹”并将其从字典中删除。到目前为止一切正常。现在,当我切换到相同的详细视图,但这次从选项卡栏上的不同选项卡访问时,首先收藏夹按钮仍然显示“从收藏夹中删除”,当我第一次单击此按钮时,它会更改为“添加到收藏夹”,然后当我再次单击它时,出现 EXC_BAD_ACCESS 错误。
这是 addToFavouritesFunction:
- (IBAction)addToFavourites:(id)sender {
NSString *type = [[NSUserDefaults standardUserDefaults]objectForKey:@"type"];
if(type == @"v") {
NSString *area = [[NSUserDefaults standardUserDefaults]objectForKey:@"area"];
NSString *ID1 = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID1"];
if([[addToFavouritesDictionary allKeys] containsObject:ID1]) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/SavedDict.data"];
[addToFavouritesDictionary removeObjectForKey:ID1];
[favouritesButton setTitle:@"+ Favourites" forState:(UIControlState)UIControlStateNormal];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"New Dictionary: %@", addToFavouritesDictionary);
} else {
NSString *ID1 = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID1"];
[addToFavouritesDictionary setObject:Name forKey:ID1];
[favouritesButton setTitle:@"- Favourites" forState:(UIControlState)UIControlStateNormal];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/SavedDict.data"];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"Mutable Dictionary: %@", addToFavouritesDictionary);
//[addToFavouritesDictionary release];
}
} else {
//NSString *area = [[NSUserDefaults standardUserDefaults]objectForKey:@"area"];
NSString *ID2 = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID2"];
if([[addToFavouritesDictionary allKeys] containsObject:ID2]) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/SavedDict.data"];
[addToFavouritesDictionary removeObjectForKey:ID2];
[favouritesButton setTitle:@"+ Favourites" forState:(UIControlState)UIControlStateNormal];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"Dictionary: %@", addToFavouritesDictionary);
} else {
NSString *ID2 = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID2"];
[addToFavouritesDictionary setObject:Name forKey:ID2];
[favouritesButton setTitle:@"- Favourites" forState:(UIControlState)UIControlStateNormal];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/SavedDict.data"];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"Mutable Dictionary: %@", addToFavouritesDictionary);
}
}
}
感谢您的帮助!
I have an addToFavourites function, which is causing an EXC_BAD_ACCESS error.
Basically, I have the Favourites View Controller with a list of all the favourites which the user has added previously, now when clicking on one of the favourites, it pushes the view to a detailViewController, this works fine. On the detail view controller, I have an add to favourites button, this button changes to a remove from favourites if it detects that the key already exists in the addToFavourites dictionary. The problem occurs when the user accesses this detailViewController from the favourites page and also from the main page which lists all the detailViews. They are able to do this as I have a tab bar.
So say I am in the detailView accessed through the favourites page, the favourites button has "remove from favourites", this is correct. But then say I click the remove from favourites button, the button changes to "add to favourites" and removes it from the dictionary. All working fine so far. Now when I switch over to the same detailView, but this time accessed from a different tab on the tab bar, firstly the favourites button still reads "remove from favourites" and when I click this button the first time, it changes to "add to favourites", then when I click it again, I get this EXC_BAD_ACCESS error.
Here is the addToFavouritesFunction:
- (IBAction)addToFavourites:(id)sender {
NSString *type = [[NSUserDefaults standardUserDefaults]objectForKey:@"type"];
if(type == @"v") {
NSString *area = [[NSUserDefaults standardUserDefaults]objectForKey:@"area"];
NSString *ID1 = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID1"];
if([[addToFavouritesDictionary allKeys] containsObject:ID1]) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/SavedDict.data"];
[addToFavouritesDictionary removeObjectForKey:ID1];
[favouritesButton setTitle:@"+ Favourites" forState:(UIControlState)UIControlStateNormal];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"New Dictionary: %@", addToFavouritesDictionary);
} else {
NSString *ID1 = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID1"];
[addToFavouritesDictionary setObject:Name forKey:ID1];
[favouritesButton setTitle:@"- Favourites" forState:(UIControlState)UIControlStateNormal];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/SavedDict.data"];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"Mutable Dictionary: %@", addToFavouritesDictionary);
//[addToFavouritesDictionary release];
}
} else {
//NSString *area = [[NSUserDefaults standardUserDefaults]objectForKey:@"area"];
NSString *ID2 = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID2"];
if([[addToFavouritesDictionary allKeys] containsObject:ID2]) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/SavedDict.data"];
[addToFavouritesDictionary removeObjectForKey:ID2];
[favouritesButton setTitle:@"+ Favourites" forState:(UIControlState)UIControlStateNormal];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"Dictionary: %@", addToFavouritesDictionary);
} else {
NSString *ID2 = [[NSUserDefaults standardUserDefaults]objectForKey:@"ID2"];
[addToFavouritesDictionary setObject:Name forKey:ID2];
[favouritesButton setTitle:@"- Favourites" forState:(UIControlState)UIControlStateNormal];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/SavedDict.data"];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(@"Mutable Dictionary: %@", addToFavouritesDictionary);
}
}
}
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用调试器检查它是否准确地显示崩溃行...然后设置断点以检查值
Check with debugger whether it shows crashing line exactly ...and set breakpoint after that to check for value
嗯...您的 addToFavouritesDictionary 对象或您的 Name 对象已被释放。您如何创建这些对象?要么你提前释放了它们,要么你一开始就没有保留它们。
Objective-C 内存管理
Well...either your addToFavouritesDictionary object or your Name object has been deallocated. How are you creating these objects? Either you are releasing them before you should, or you haven't retained them in the first place.
Objective-C Memory Management
您可能不应该使用以下行:
您在这里所做的是将变量
type
的指针与字符串@"v"
的指针进行比较。相反,您应该使用比较字符串的内容的方法:编辑
保持标题相同的最简单方法是将文本存储在 NSUserDefaults 中。每当按下按钮时,您都可以执行如下操作:
然后,当视图加载时,您将执行如下操作:
You should probably not be using the line:
What you are doing here is comparing the pointer for the variable
type
to the pointer for the string@"v"
. Instead, you should use a method that compares the contents of the string:EDIT
The easiest way to keep your titles the same would be to store the text in NSUserDefaults. You could do something like below whenever the button is pressed:
Then you would execute something like this when the view loads: