【问题标题】:Can't get NSMutableDictionary to work (xcode)无法让 NSMutableDictionary 工作(xcode)
【发布时间】:2017-01-21 12:30:05
【问题描述】:

我的 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”。

【问题讨论】:

  • 您的mmap 未初始化。 @implementation 下的代码没有意义,它没有被执行(至少在你期望的时候)。将您的代码放入初始化程序或某些方法中。
  • 我把它移到了一个 -(void)construct,我在分配后运行它。这没有帮助:/
  • 好吧!修复! :D 我不得不使用 self.mmap = [[NSMutableDictionary alloc] init];在 alloc 之后调用的 void 函数内。没有自我。首先它没有用。

标签: ios objective-c xcode nsmutabledictionary


【解决方案1】:

您需要重写 MyClass init 方法并在其中初始化 MyMap usemap = [[MyMap alloc] init]。然后重写 MyMap init 方法并在其中初始化 mmap self.mmap = [[NSMutableDictionary alloc] init]

在覆盖 init 方法的 MyClass 中完成其余工作。这是一个粗略的草图:

.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

希望对你有帮助!

【讨论】:

    【解决方案2】:

    试试这个:

    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 到键:testkey

    【讨论】:

    • 谢谢,但我不能像这样改变结构,知道为什么这样行,但我的不行吗?
    • @user2105624,这行MyMap* usemap = [MyMap alloc];没有初始化MyMap实例,alloc只是分配存储。
    • 我很困惑,其他一切正常,我有 NSMutableData* 等正常工作。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2017-07-02
      • 2023-03-06
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      相关资源
      最近更新 更多