-3

I use an NSMutableDictionary to save objects. Now I wanna access these objects in Swift.

But when I create var player in Swift, the saved objects are no longer accessible when I invoke the property. How can I solve this problem?

I tried to get access like described here. I can invoke the method player.remoteAccess in Swift, but the Dictionary is empty. In Objective-C I have access to the content.

Thank you :)

PlayerClass.h

@interface PlayerClass : NSObject {
}

@property (nonatomic, readwrite) NSMutableDictionary *property;
-(void)playTextToSpeech;
-(void)remoteAccess;

PlayerClass.m

@implementation PlayerClass

@synthesize property = _property;


-(id)init
{
  self = [super init];
  if (self != nil)
    {       
        _property = [[NSMutableDictionary alloc] init];         
    }
return self;
}

-(void)playTextToSpeech
{
     _property[@"module_id"] = [[AudioPlayer alloc] initWithSocket:_socket moduleID:[@"module_id"]];
    NSLog(@"property: %@", _property.description) // objects accessible
}

-(void)remoteAccess
{
   NSLog(@"remoteAccess method ran");
   NSLog(@"%@", _property.description); 
}

Swift

@objc class AudioPlayer: NSObject, AVAudioPlayerDelegate {

var player: PlayerClass = PlayerClass()
self.invokePlayerClass()

    func invokePlayerClass() {
      player.remoteAccess() // empty 
    }
}
Community
  • 1
  • 1
  • 1
    You're going to have to post more code for this to make sense... – Mike Pollard Oct 29 '15 at 16:11
  • You have declared a variable called `property` in your header. Ids this inside the class interface or outside? In the .m file you seem to have an instance variable called `_property` which is not the same. In Swift you access a **property** confusingly called `property` which is not the same as either of the two variables. – JeremyP Oct 29 '15 at 16:59

1 Answers1

0

Not absolutely certain what you have done, but it looks like you are confusing properties and instance variables. Instance variables don't really exist in Swift. I would expect your Objective-C to look something like this:

@interface PlayerClass: NSObject

@property (nonatomic, strong) NSMutableDictionary* property;

-(void) playTextToSpeech;

@end

@implementation PlayerClass

@synthesize property = _property;

-(id) init
{
    self = [super init];
    if (self != nil)
    {
        _property = [[NSMutableDictionary alloc] init];
    }
    return self;
}

-(void)playTextToSpeech
{
    [self property][@"module_id"] = [[AudioPlayer alloc] initWithSocket:_socket moduleID:[@"module_id"]];
    NSLog(@"property: %@", _property.description); // objects accessible
}

@end

If it looks something like that, it'll probably be OK in Swift.

JeremyP
  • 84,577
  • 15
  • 123
  • 161