0

我创建了一个 Singleton 类来充当音频播放器,它将在启动时开始播放音频,并且可以从另一个类中关闭。

在另一个班级,当我打电话时

[[Singleton singleton] audioPlayer stop];

它给了我错误

'Expected :'

我了解 OOP,但我认为我从未尝试过访问对象的对象。有任何想法吗?

//  Singleton.m


#import "Singleton.h"

@implementation Singleton

#pragma mark Singleton Methods

+ (id)sharedManager {
static Singleton *singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    singleton = [[self alloc] init];
});
return singleton;
}

- (id)init {
if (self = [super init]) {

    boy = false;
    girl = false;

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Mathletics Theme.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -1;
    audioPlayer.volume = 0.85f;

    if (audioPlayer == nil)
        NSLog([error description]);
    else
        [audioPlayer play];

    audioPlayer.numberOfLoops = -1;

    if (audioPlayer == nil)
        NSLog([error description]);
    else
        [audioPlayer play];
}
return self;
}

@end





//  Singleton.h

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

@interface Singleton : NSObject {

AVAudioPlayer *audioPlayer;

}

@property (nonatomic, retain) AVAudioPlayer *audioPlayer;

+ (id)singleton;

@end
4

2 回答 2

0

在你的 Singleton 类中,你可能有这样的方法+ (MySingleton *) instance

所以使用[[Singleton instance] audioPlayer stop];

或者

Singleton *singleton = [Singleton instance];
[singleton.audioPlayer stop];

带有 NSString 的 MySingleton 示例

MySingleton *mySingleton = [MySingleton instance];
mySingleton.myString = @"hi sigletone";    
NSLog(@"%@",mySingleton.myString);

示例 MySingleton.h

@interface MySingleton : NSObject

   @property (nonatomic,retain) NSString *myString;

  + (MySingleton *) instance;

@end

示例 MySingleton.m

#import "MySingleton.h"

@implementation MySingleton

- (id) initSingleton
{
    if ((self = [super init]))
    {
        // Initialization code here.
    }

    return self;
}

+ (MySingleton *) instance
{
    // Persistent instance.
    static MySingleton *_default = nil;

    // Small optimization to avoid wasting time after the
    // singleton being initialized.
    if (_default != nil)
    {
        return _default;
    }

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
    // Allocates once with Grand Central Dispatch (GCD) routine.
    // It's thread safe.
    static dispatch_once_t safer;
    dispatch_once(&safer, ^(void)
                  {
                      _default = [[MySingleton alloc] initSingleton];
                  });
#else
    // Allocates once using the old approach, it's slower.
    // It's thread safe.
    @synchronized([MySingleton class])
    {
        // The synchronized instruction will make sure,
        // that only one thread will access this point at a time.
        if (_default == nil)
        {
            _default = [[MySingleton alloc] initSingleton];
        }
    }
#endif
    return _default;
}

@end
于 2012-11-28T08:11:09.987 回答
0

你错过了括号 [ ]

[[[Singleton singleton] audioPlayer] stop];

但从您的代码来看,这似乎不是唯一的问题

[[singleton sharedManager /*your method].audioPlayer stop];

audioPlayer 必须是单例的属性才能使 var 可用

@property(readonly) AVPlayer *audioPlayer;
于 2012-11-28T08:45:16.903 回答