错误:从不同方法执行选择器时出现 EXC_BAD_ACCESS
Posted
技术标签:
【中文标题】错误:从不同方法执行选择器时出现 EXC_BAD_ACCESS【英文标题】:Error: EXC_BAD_ACCESS when performing selector from different methods 【发布时间】:2014-03-03 13:08:00 【问题描述】:当从“textViewDidEndEditing”调用时,我的“updateNotes”方法有效,但从“handleRightSwipe”(UISwipeGestureRecognizer)调用时无效。
我在这两种方法中都使用:
[self performSelector:selector(updateNotes:)];
方法如下:
- (void)updateNotes: (id)sender
Note *noteAtIndex = [self.notes objectAtIndex:countDown];
NSLog(@"noteAtIndex is %@", noteAtIndex);
NSLog(@"noteAtIndex.dayNote is %@", noteAtIndex.dayNote);
//self.noteLabel.text = noteAtIndex.dayNote;
[self.noteTextView setText:noteAtIndex.dayNote];
有什么建议吗?
按照建议添加了 NSLog:
- (void)updateNotes: (id)sender
NSLog(@"self is %@", self);
NSLog(@"self.notes is %@", self.notes);
Note *noteAtIndex = [self.notes objectAtIndex:countDown];
NSLog(@"noteAtIndex is %@", noteAtIndex);
NSLog(@"noteAtIndex.dayNote is %@", noteAtIndex.dayNote);
//self.noteLabel.text = noteAtIndex.dayNote;
NSLog(@"Stack trace : %@",[NSThread callStackSymbols]);
[self.noteTextView setText:noteAtIndex.dayNote];
这是日志:
2014-03-03 07:44:07.272 90 Day Track[8194:70b] left swipe detected
2014-03-03 07:44:07.273 90 Day Track[8194:70b] countDown is 1
2014-03-03 07:44:09.760 90 Day Track[8194:70b] notes array is (
"<Note: 0x8e58e40> (entity: Note; id: 0x8e57bc0 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p1> ; data: <fault>)",
"<Note: 0x8e59080> (entity: Note; id: 0x8e57bd0 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p2> ; data: <fault>)",
"<Note: 0x8e590c0> (entity: Note; id: 0x8e57be0 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p3> ; data: <fault>)",
"<Note: 0x8e59100> (entity: Note; id: 0x8e57bf0 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p4> ; data: <fault>)",
"<Note: 0x8e59140> (entity: Note; id: 0x8e57c00 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p5> ; data: <fault>)",
"<Note: 0x8e59180> (entity: Note; id: 0x8e57c10 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p6> ; data: <fault>)",
"<Note: 0x8e591c0> (entity: Note; id: 0x8e57c20 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p7> ; data: <fault>)",
"<Note: 0x8e59200> (entity: Note; id: 0x8e57c30 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p8> ; data: <fault>)",
"<Note: 0x8e59240> (entity: Note; id: 0x8e57c40 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p9> ; data: <fault>)",
"<Note: 0x8e59280> (entity: Note; id: 0x8e57c50 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p10> ; data: <fault>)",
"<Note: 0x8e592c0> (entity: Note; id: 0x8e57c60 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p11> ; data: <fault>)",
"<Note: 0x8e59300> (entity: Note; id: 0x8e57c70 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p12> ; data: <fault>)",
"<Note: 0x8e59340> (entity: Note; id: 0x8e57c80 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p13> ; data: <fault>)",
"<Note: 0x8e59380> (entity: Note; id: 0x8e57c90 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p14> ; data: <fault>)",
"<Note: 0x8e593c0> (entity: Note; id: 0x8e57ca0 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p15> ; data: <fault>)",
"<Note: 0x8e59400> (entity: Note; id: 0x8e57cb0 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p16> ; data: <fault>)",
"<Note: 0x8e59440> (entity: Note; id: 0x8e57cc0 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p17> ; data: <fault>)",
"<Note: 0x8e59480> (entity: Note; id: 0x8e57cd0 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p18> ; data: <fault>)",
"<Note: 0x8e594c0> (entity: Note; id: 0x8e57ce0 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p19> ; data: <fault>)",
"<Note: 0x8e59500> (entity: Note; id: 0x8e57cf0 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p20> ; data: <fault>)",
"<Note: 0x8e59540> (entity: Note; id: 0x8e57d00 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p21> ; data: <fault>)",
"<Note: 0x8e59580> (entity: Note; id: 0x8e57d10 <x-coredata://F27E8E39-7579-4CB5-9637-683892414E95/Note/p22> ; data: <fault>)"
)
2014-03-03 07:44:12.434 90 Day Track[8194:70b] newNote is <Note: 0x8b8a4c0> (entity: Note; id: 0x8b8a4f0 <x-coredata:///Note/tFAB4D984-C9A9-4322-96F4-36AA1557F8E72> ; data:
day = nil;
dayNote = Notes;
)
2014-03-03 07:44:12.435 90 Day Track[8194:70b] newDay is <Day: 0x8b8fbe0> (entity: Day; id: 0x8b8e7f0 <x-coredata:///Day/tFAB4D984-C9A9-4322-96F4-36AA1557F8E73> ; data:
dayNumber = 1;
note = nil;
)
2014-03-03 07:44:15.860 90 Day Track[8194:70b] right swipe detected
2014-03-03 07:44:15.861 90 Day Track[8194:70b] countDown is 2
(lldb)
【问题讨论】:
NSZombies?有些东西似乎是空的(已经发布)。 显示堆栈跟踪?在updateNotes
方法 NSLog self
中的任何其他代码之前,然后是 self.notes
的另一个 NSLog 以查看其中一个或另一个是否可访问。
添加了 NSLogs 和堆栈跟踪。
我实际上是指崩溃时的堆栈跟踪。 :) 但是,从您发布的日志来看,您的方法似乎根本没有被调用......而不是提供“错误访问”。我认为selector(updateNotes:)
是一个错字,实际上是@selector(updateNotes:)
,对吧?
是的,“选择器”是一个错字。我有 ”@”。我是这方面的新手,你可以肯定地说,所以我不知道如何获取堆栈跟踪。当我再次运行它时,它在“updateNotes”方法和“@selector”调用中的线程 1 上显示“EXC_BAD_ACCESS (code=1, address=0xf3c0570d)。
【参考方案1】:
performSelector:
仅适用于无参数的方法。 performSelector:withObject:
使用一个参数的方法,这就是你所拥有的 (updateNotes:
)。
(另外,你为什么还要使用带有固定选择器的performSelector...
?为什么不直接拨打电话?)
【讨论】:
以上是关于错误:从不同方法执行选择器时出现 EXC_BAD_ACCESS的主要内容,如果未能解决你的问题,请参考以下文章