我可以在类之间传递基本数据,但是当我尝试从我的NSString*中传递UIApplicationDelegate时,我得到了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);


OtherObject.m:

// Returns a Zombie object & EXC_BAD_ACCESS:
NSString *currentNoteName = [appDelegate currentNoteName];

最佳答案

如果不使用ARC,则必须使用retain属性:

@property (atomic, retain) NSString *currentNoteName;


并使用setter为它分配一个值:

self.currentNoteName = [NSString stringWithCString: ...];


并且不要忘记在deallocAppDelegate实现中释放此ivar的实例:

- (void) dealloc {
  [currentNoteName release], currentNoteName = nil;
  [super dealloc];
}

10-06 12:37