我可以在类之间传递基本数据,但是当我尝试从我的 UIApplicationDelegate
传递一个 NSString*
时,我得到一个 EXC_BAD_ACCESS
/NSZombie
.
为了返回 NSObject
,我需要做些什么特别的事情吗?这与线程有关吗? (我认为属性上的 atomic
设置会解决这个问题?)
AppDelegate.h:
@interface AppDelegate : NSObject <UIApplicationDelegate> {
NSString * currentNoteName;
}
@property (atomic, assign) NSString *currentNoteName;
@end
AppDelegate.m:
- (void)timerCallback:(NSTimer *)timer {
currentNoteName = [NSString stringWithCString:(tone->freq).c_str() encoding:NSUTF8StringEncoding];
// This works:
NSLog(@"Current Note Name in timerCallback: %@", currentNoteName);
其他对象.m:
// Returns a Zombie object & EXC_BAD_ACCESS:
NSString *currentNoteName = [appDelegate currentNoteName];
最佳答案
如果不使用 ARC,则必须使用保留属性:
@property (atomic, retain) NSString *currentNoteName;
并使用setter为其分配一个值:
self.currentNoteName = [NSString stringWithCString: ...];
不要忘记在 AppDelegate
的 dealloc
实现中释放这个 ivar 的实例:
- (void) dealloc {
[currentNoteName release], currentNoteName = nil;
[super dealloc];
}
关于ios - 从另一个对象调用时僵尸 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8213183/