从Swift中保存在NSDictionary中的Access对象

时间:2015-10-29 16:10:14

标签: ios swift nsdictionary storage

我使用NSMutableDictionary来保存对象。 现在我想在Swift中访问这些对象。

但是当我在Swift中创建var player时,当我调用属性时,不再可以访问保存的对象。我该如何解决这个问题?

我试图获得如here所述的访问权限。我可以在Swift中调用方法player.remoteAccess,但是Dictionary是空的。在Objective-C中,我可以访问内容。

谢谢:)

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); 
}

夫特

@objc class AudioPlayer: NSObject, AVAudioPlayerDelegate {

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

    func invokePlayerClass() {
      player.remoteAccess() // empty 
    }
}

1 个答案:

答案 0 :(得分:0)

不完全确定你做了什么,但看起来你混淆了属性和实例变量。实例变量在Swift中确实不存在。我希望你的Objective-C看起来像这样:

@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

如果它看起来像那样,它在Swift中可能会好的。