无法让 NSMutableDictionary 工作 (xcode)
Can't get NSMutableDictionary to work (xcode)
我的 Objective-C 体验不好,如果有人可以帮助我,我会很高兴。
周围还有一些其他代码,但我相信我已将其简化为问题所在。我想做的是用一个键保存一个双精度数(打包到 NSNumber),下面是我的代码:
.h
@interface MyClass : something
{
MyMap* usemap;
}
@end
@interface MyMap : something
@property (atomic, strong) NSMutableDictionary* mmap;
@end
.m
@implementation MyMap
@class MyClass;
NSMutableDictionary* mmap = [[NSMutableDictionary alloc] init];
@end
@implementation MyClass
MyMap* usemap = [MyMap alloc];
//-void { switch() { case x:
NSString* key = @"testkey";
NSNumber* value = [NSNumber numberWithDouble:1];
NSLog(@"Saving value: %@ to key: %@",value,key);
[usemap.mmap setObject:value forKey:key];
NSNumber* get = [usemap.mmap objectForKey:key];
NSLog(@"Saved value: %@ to key: %@",get,key);
现在打印:
Saving value: 1 to key: testkey
Saved value: (null) to key: testkey
我错过了什么?它应该保存双数“1”。
试试这个:
MyMap.h:
#import <UIKit/UIKit.h>
@interface MyMap : NSObject
@property (atomic, strong) NSMutableDictionary* mmap;
@end
MyMap.m:
#import "MyMap.h"
@interface MyMap ()
@end
@implementation MyMap
@end
ViewController.m:
MyMap *usemap = [[MyMap alloc] init];
usemap.mmap = [[NSMutableDictionary alloc] init];
NSString* key = @"testkey";
NSNumber* value = [NSNumber numberWithDouble:1];
NSLog(@"Saving value: %@ to key: %@",value,key);
[usemap.mmap setObject:value forKey:key];
NSNumber* get = [usemap.mmap objectForKey:key];
NSLog(@"Saved value: %@ to key: %@",get,key);
结果:
2017-01-21 20:48:59.563 TestOC[4161:50263] Saving value: 1 to key: testkey
2017-01-21 20:48:59.564 TestOC[4161:50263] Saved value: 1 to key: testkey
您需要覆盖 MyClass init
方法并在其中初始化 MyMap usemap = [[MyMap alloc] init]
。然后覆盖 MyMap init 方法并在其中初始化 mmap self.mmap = [[NSMutableDictionary alloc] init]
.
在 MyClass 覆盖的 init
方法中完成其余工作。这是一个粗略的草图:
.h
@interface MyClass : something
{
MyMap* usemap;
}
@end
@interface MyMap : something
@property (atomic, strong) NSMutableDictionary* mmap;
@end
.m
@implementation MyMap
@class MyClass;
- (instancetype)init {
if ((self = [super init]))
self.mmap = [[NSMutableDictionary alloc] init];
return self;
}
@end
@implementation MyClass
- (instancetype)init {
if ((self = [super init]))
{
usemap = [[MyMap alloc] init];
NSString* key = @"testkey";
NSNumber* value = [NSNumber numberWithDouble:1];
NSLog(@"Saving value: %@ to key: %@",value,key);
[usemap.mmap setObject:value forKey:key];
NSNumber* get = [usemap.mmap objectForKey:key];
NSLog(@"Saved value: %@ to key: %@",get,key);
}
return self;
}
@end
希望对您有所帮助!
设置对象的内部结构是使用 -init
方法完成的。
@implementation MyMap
@class MyClass;
// NO NO NO! don't even ask what this does
// NSMutableDictionary* mmap = [[NSMutableDictionary alloc] init];
// YES: Use -init to setup the initial state of an object.
- (instancetype)init {
self = [super init];
if (self != nil) {
// NOTE: _mmap is the instance variable for the property self.mmap.
_mmap = [[NSMutableDictionary alloc] init];
}
return self;
}
@end
我的 Objective-C 体验不好,如果有人可以帮助我,我会很高兴。
周围还有一些其他代码,但我相信我已将其简化为问题所在。我想做的是用一个键保存一个双精度数(打包到 NSNumber),下面是我的代码:
.h
@interface MyClass : something
{
MyMap* usemap;
}
@end
@interface MyMap : something
@property (atomic, strong) NSMutableDictionary* mmap;
@end
.m
@implementation MyMap
@class MyClass;
NSMutableDictionary* mmap = [[NSMutableDictionary alloc] init];
@end
@implementation MyClass
MyMap* usemap = [MyMap alloc];
//-void { switch() { case x:
NSString* key = @"testkey";
NSNumber* value = [NSNumber numberWithDouble:1];
NSLog(@"Saving value: %@ to key: %@",value,key);
[usemap.mmap setObject:value forKey:key];
NSNumber* get = [usemap.mmap objectForKey:key];
NSLog(@"Saved value: %@ to key: %@",get,key);
现在打印:
Saving value: 1 to key: testkey
Saved value: (null) to key: testkey
我错过了什么?它应该保存双数“1”。
试试这个:
MyMap.h:
#import <UIKit/UIKit.h>
@interface MyMap : NSObject
@property (atomic, strong) NSMutableDictionary* mmap;
@end
MyMap.m:
#import "MyMap.h"
@interface MyMap ()
@end
@implementation MyMap
@end
ViewController.m:
MyMap *usemap = [[MyMap alloc] init];
usemap.mmap = [[NSMutableDictionary alloc] init];
NSString* key = @"testkey";
NSNumber* value = [NSNumber numberWithDouble:1];
NSLog(@"Saving value: %@ to key: %@",value,key);
[usemap.mmap setObject:value forKey:key];
NSNumber* get = [usemap.mmap objectForKey:key];
NSLog(@"Saved value: %@ to key: %@",get,key);
结果:
2017-01-21 20:48:59.563 TestOC[4161:50263] Saving value: 1 to key: testkey
2017-01-21 20:48:59.564 TestOC[4161:50263] Saved value: 1 to key: testkey
您需要覆盖 MyClass init
方法并在其中初始化 MyMap usemap = [[MyMap alloc] init]
。然后覆盖 MyMap init 方法并在其中初始化 mmap self.mmap = [[NSMutableDictionary alloc] init]
.
在 MyClass 覆盖的 init
方法中完成其余工作。这是一个粗略的草图:
.h
@interface MyClass : something
{
MyMap* usemap;
}
@end
@interface MyMap : something
@property (atomic, strong) NSMutableDictionary* mmap;
@end
.m
@implementation MyMap
@class MyClass;
- (instancetype)init {
if ((self = [super init]))
self.mmap = [[NSMutableDictionary alloc] init];
return self;
}
@end
@implementation MyClass
- (instancetype)init {
if ((self = [super init]))
{
usemap = [[MyMap alloc] init];
NSString* key = @"testkey";
NSNumber* value = [NSNumber numberWithDouble:1];
NSLog(@"Saving value: %@ to key: %@",value,key);
[usemap.mmap setObject:value forKey:key];
NSNumber* get = [usemap.mmap objectForKey:key];
NSLog(@"Saved value: %@ to key: %@",get,key);
}
return self;
}
@end
希望对您有所帮助!
设置对象的内部结构是使用 -init
方法完成的。
@implementation MyMap
@class MyClass;
// NO NO NO! don't even ask what this does
// NSMutableDictionary* mmap = [[NSMutableDictionary alloc] init];
// YES: Use -init to setup the initial state of an object.
- (instancetype)init {
self = [super init];
if (self != nil) {
// NOTE: _mmap is the instance variable for the property self.mmap.
_mmap = [[NSMutableDictionary alloc] init];
}
return self;
}
@end