我正在尝试编写一个简单的iOS
程序,该程序将在插入/拔出耳机时发出通知。我是Objective-C
和iOS
的新手,很多代码是由其他人编写的,我现在正尝试对其进行调整,因此在解决此问题时遇到了问题。代码在这里:
AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
static void onHeadsetChange(void *, AudioServicesPropertyID, UInt32, const void *);
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.headsetOnHandler = self.headsetOffHandler = ^{};
NSLog(@"%lu", AudioSessionInitialize(NULL, NULL, NULL, NULL));
NSLog(@"%lu", AudioSessionSetActive(true));
NSLog(@"%lu", AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, &onHeadsetChange, (__bridge void *)self));
return YES;
}
static void onHeadsetChange(void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue) {
if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;
NSLog(@"The headset changed");
AppDelegate *const delegate = (__bridge AppDelegate *)inUserData;
const CFDictionaryRef routeChangeDictionary = inPropertyValue;
const CFNumberRef routeChangeReasonRef = CFDictionaryGetValue(routeChangeDictionary, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));
SInt32 routeChangeReason;
CFNumberGetValue(routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
switch (routeChangeReason) {
case kAudioSessionRouteChangeReason_NewDeviceAvailable:
//NSLog([delegate description]);
[delegate headsetOnHandler];
break;
case kAudioSessionRouteChangeReason_OldDeviceUnavailable:
[delegate headsetOffHandler];
break;
default:
break;
}
}
@end
ViewController.m
#import "ViewController.h"
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
AppDelegate *delegate = (AppDelegate *)([UIApplication sharedApplication].delegate);
delegate.headsetOnHandler = ^{ self.view.backgroundColor = [UIColor greenColor];};
delegate.headsetOffHandler = ^{ self.view.backgroundColor = [UIColor redColor];};
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我正在获取
EXC_BAD_ACCESS error, code=1 at [delegate headsetOnHandler] and [delegate headsetOffHandler]
。如果需要为此添加.h
文件,我会的,如果还有其他遗漏的地方,请告诉我。编辑:
我添加了人们认为应该添加的声明:
@property (readwrite, assign) void (^headsetOnHandler)(void);
@property (readwrite, assign) void (^headsetOffHandler)(void);
最佳答案
如何声明headsetOnHandler
和headsetOffHandler
属性?
如果是assign
或retain
,则可能是问题的根源。由于它们是块,因此它必须是copy
(即使在某些情况下,在ARC下)。如果它们是atomic
和retain
,那么您的getter将有效地在getter中调用[[block retain] autorelease]
,这将解释您的崩溃。
有点远射。
请注意,ARC会稍微改变这种行为,在某些情况下会自动处理这些块,但是-IIRC-在这种情况下,您仍然需要对其进行
copy
编码。
关于ios - Objective-C中的EXC_BAD_ACCESS错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16683994/