无法使NSMutableDictionary工作(xcode)

时间:2017-01-21 12:30:05

标签: ios objective-c xcode nsmutabledictionary

我的Objective-C经验并不好,如果有人可以提供帮助,我很乐意。

还有一些其他代码,但我相信我简化了这个问题。我想要做的是用密钥保存一个double(打包到NSNumber),下面是我的代码:

·H

<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>

的.m

@interface MyClass : something
{
    MyMap* usemap;
}
@end

@interface MyMap : something
@property (atomic, strong) NSMutableDictionary* mmap;
@end

现在打印:

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

我错过了什么?它本应保存双号“1”。

3 个答案:

答案 0 :(得分:1)

您需要覆盖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

希望它有所帮助!

答案 1 :(得分:0)

试试这个:

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]保存值:1到键:testkey
  2017-01-21 20:48:59.564 TestOC [4161:50263]保存的值:1到key:testkey

答案 2 :(得分:0)

使用-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