• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 无法在单例中向 NSMutableDictionary 添加条目

[复制链接]
菜鸟教程小白 发表于 2022-12-12 22:50:41 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个带有 NSMutableDictionary 的单例。我想从我的一个 View 中向该字典添加一个条目。由于我无法理解它不起作用并且我收到“NSDictionary setObject:forKey: unrecognized selector sent to instance”错误的原因。这似乎不应该那么难,但我找不到问题的答案。

所以我在 .xib 中连接了一个按钮来调用 createKey 方法和 kablooey。我还进行了测试以确保字典存在并且确实存在。

这是我的单例标题:

#import <Foundation/Foundation.h>

@interface SharedAppData : NSObject <NSCoding>
{
    NSMutableDictionary *apiKeyDictionary;
}

+ (SharedAppData *)sharedStore;

@property (nonatomic, copy) NSMutableDictionary *apiKeyDictionary;

-(BOOL)saveChanges;

@end

我的单例实现(重要部分)

 @interface SharedAppData()
    @end

    @implementation SharedAppData

    @synthesize apiKeyDictionary;

    static SharedAppData *sharedStore = nil;

+(SharedAppData*)sharedStore {

        @synchronized(self){
        if(sharedStore == nil){
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *testFile = [documentsDirectory stringByAppendingPathComponent"testfile.sav"];
            Boolean fileExists = [[NSFileManager defaultManager] fileExistsAtPath:testFile];

            if(fileExists) {
                sharedStore = [NSKeyedUnarchiver unarchiveObjectWithFile:testFile];
            }
            else{
                sharedStore = [[super allocWithZone:NULL] init];
            }

            [sharedStore setSaveFile:testFile];
        }

            return sharedStore;
        }
}

    - (id)init {
        if (self = [super init]) {
            apiKeyDictionary = [[NSMutableDictionary alloc] init];      
        }
        return self;
    }

在我的 View Controller 标题中...

#import <UIKit/UIKit.h>
#import "SharedAppData.h"

@interface AddKeyViewController : UIViewController <UITextFieldDelegate>
{
    UIButton *addKey;
}

@property (weak, nonatomic) IBOutlet UITextField *apiName;
@property (weak, nonatomic) IBOutlet UITextField *apiKey;

-(IBAction)createKeyid)sender;

@end

查看 Controller 实现:

#import "AddKeyViewController.h"
#import "SharedAppData.h"

@interface AddKeyViewController ()

@end

@implementation AddKeyViewController

@synthesize apiName, apiKey, toolbar;

-(IBAction)createKeyid)sender {

    NSString *name = [apiName text];
    NSString *key = [apiKey text];

    [[[SharedAppData sharedStore] apiKeyDictionary] setObject:key forKey:name];
}

@end



Best Answer-推荐答案


您的 apiKeyDictionary 属性设置为 copy。这会将 copy 消息发送到您在 init 方法中创建的 NSMutableDictionary 实例 - 返回的不是 NSMutableDictionary 而是NSDictionary。改为 strongretain

关于ios - 无法在单例中向 NSMutableDictionary 添加条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12632891/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap