【问题标题】:Bad Access code 1 with simple NSObject带有简单 NSObject 的错误访问代码 1
【发布时间】:2014-07-23 20:13:05
【问题描述】:

我正在创建一个自定义对象,目前非常简单。您将在下面找到该对象的 .h。

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@interface LAZMessagePlayer : NSObject <AVAudioPlayerDelegate>

- (void)play:(UIView*)viewThatWasPressed;
- (void)pause;
- (BOOL)isPlaying;
@end

以及对象对应的.m

#import "LAZMessagePlayer.h"
@implementation LAZMessagePlayer {
    AVAudioPlayer* myPlayer;
    UIView* currentViewPlaying;
}

-(id)init{
    NSLog(@"init audio");
    NSURL* src = [NSURL fileURLWithPath:[NSString stringWithFormat:@"/Users/Developer/Downloads/317.mp3", [[NSBundle mainBundle] resourcePath]]];
    NSError* error;
    myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:src error:&error];
}

-(void)play:(UIView*)viewThatWasPressed {
    NSLog(@"play");
    currentViewPlaying = viewThatWasPressed;
    currentViewPlaying.hidden = YES;
    [myPlayer play];
}

-(void)pause {
    NSLog(@"pause");
    currentViewPlaying.hidden = NO;
    [myPlayer pause];
}

-(void)audioPlayerDidFinishPlaying:
(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSLog(@"done");
    currentViewPlaying.hidden = NO;
}

-(BOOL)isPlaying {
    return myPlayer.playing;
}

@end

在我的 ViewController 内部,我已经在 Controller.m 的顶部声明了一个私有变量,就像这样,

#import "LAZMessagesViewController.h"
@interface LAZMessagesViewController () {
    LAZMessagePlayer* myPlayer;
}

在我的 viewDidLoad 中,我分配/初始化 myPlayer。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarExtensionViewDelegate = self;
    // Do any additional setup after loading the view.
    myPlayer = [[LAZMessagePlayer alloc] init];
}

稍后在我的 Tap 处理程序中,我第一次尝试引用 myPlayer 时收到 BAD ACCESS code = 1,这是中断的第一行

if([myPlayer isPlaying])...

我熟悉 c++ 内存管理,但对 Objective-C 完全陌生。谁能指出我明显的失误。

谢谢。

【问题讨论】:

    标签: ios objective-c memory-management


    【解决方案1】:

    自己解决了。我需要在 Object.h 内部:

    self = [super init];
    .
    .
    .
    return self;
    

    关于否决票。我以为我在发布之前彻底研究了我的问题。我还认为我提供了所有必要的信息来帮助解决问题。您能否至少评论一下为什么投反对票,以便我知道下次如何避免它?谢谢。

    【讨论】:

      猜你喜欢
      • 2012-01-20
      • 2014-04-01
      • 1970-01-01
      • 2014-08-16
      • 2011-09-21
      • 1970-01-01
      • 2020-03-19
      • 2016-07-19
      • 2013-01-08
      相关资源
      最近更新 更多