Ios 从Swift访问保存在NSDictionary中的对象
我使用NSMutableDictionary来保存对象。 现在我想在Swift中访问这些对象 但是当我在Swift中创建var player时,当我调用属性时,保存的对象将不再可访问。我怎样才能解决这个问题 我试图像上面描述的那样访问。我可以在Swift中调用方法player.remoteAccess,但字典是空的。在Objective-C中,我可以访问内容 谢谢各位: PlayerClass.hIos 从Swift访问保存在NSDictionary中的对象,ios,swift,nsdictionary,storage,Ios,Swift,Nsdictionary,Storage,我使用NSMutableDictionary来保存对象。 现在我想在Swift中访问这些对象 但是当我在Swift中创建var player时,当我调用属性时,保存的对象将不再可访问。我怎样才能解决这个问题 我试图像上面描述的那样访问。我可以在Swift中调用方法player.remoteAccess,但字典是空的。在Objective-C中,我可以访问内容 谢谢各位: PlayerClass.h @interface PlayerClass : NSObject { } @property
@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中并不存在。我希望您的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中可能就可以了。您需要发布更多代码来说明这一点……您已经在标头中声明了一个名为property的变量。是在类接口内部还是外部?在.m文件中,您似乎有一个名为_property的实例变量,它是不同的。在Swift中,您访问一个名为property的属性,该属性与两个变量中的任何一个都不相同。
@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