NSDictionary读取数据

2024-01-15

我现在已经尝试让它工作几个小时,但就是无法成功。

我有以下代码:

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:  

[NSArray arrayWithObjects:@"currentGame1", @"currentGameType1", @"currentGameQuestion1", @"currentGameRightAnswers1", @"currentGameType1", @"numberOfType0Games1", @"type0Results1", @"numberOfType1Games1", @"type1Results1",@"numberOfType2Games1", @"type2Results1",nil], @"Player1",  

[NSArray arrayWithObjects:@"currentGame2", @"currentGameType2", @"currentGameQuestion2", @"currentGameRightAnswers2", @"currentGameType2", @"numberOfType0Games2", @"type0Results2", @"numberOfType1Games2", @"type1Results2",@"numberOfType2Games2", @"type2Results2",nil], @"Player2",  

[NSArray arrayWithObjects:@"currentGame3", @"currentGameType3", @"currentGameQuestion3", @"currentGameRightAnswers3", @"currentGameType3", @"numberOfType0Games3", @"type0Results3", @"numberOfType1Games3", @"type1Results3",@"numberOfType2Games3", @"type2Results3",nil], @"Player3",nil];  
[dict writeToFile:@"/Users/MikaelB/Desktop/xxxxPlayer.plist" atomically: TRUE];  

NSMutableDictionary *readDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/Users/MikaelB/Desktop/xxxxPlayer.plist"];  

NSLog(@"readDict: %@", readDict);  
NSLog(@"= = = = = = = = = = = = = = = = = = =");

for (NSArray *key in [readDict allKeysForObject:@"Player6"]) {  
    NSLog(@"Key: %@", key);  
}  

for 循环只是我尝试从字典中提取数据所做的测试的一部分,也是我测试过的许多不同方法之一。

我的问题是是否有好人可以向我展示如何提取记录(键+对象)并对其进行 NSLog?

Cheers


如果您尝试为多个玩家保存游戏状态,那么您的做法可能是错误的。我不想阻碍您当前的进步,但我想指出一些改进的途径。

第一点: NSDictionary对象(以及 plist)可以在树结构中存储数据,因此不需要为每个用户拥有一组不同的键:

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSArray arrayWithObjects:@"currentGame", @"currentGameType", ..., nil],
    @"player1",

    [NSArray arrayWithObjects:@"currentGame", @"currentGameType", ..., nil],
    @"player2",

    ..., nil];

第二点:更进一步,我认为你真正想要的是另一个完整的字典对于每个玩家:

NSMutableDictionary * playerInfo;
playerInfo = [NSMutableDictionary dictionaryWithCapcity:0];

[playerInfo setObject:@"1" forKey:@"currentGame"];
[playerInfo setObject:@"2" forKey:@"currentGameType"];
[playerInfo setObject:@"what's up?" forKey:@"currentGameQuestion"];
...

NSMutableDictionary * allPlayers;
allPlayers = [NSMutableDictionary dictionaryWithCapcity:0];

NSInteger playerNum;
for (playerNum = 1; playerNum <= 6; playerNum++) {
    NSString * key = [NSString stringWithFormat:@"Player%d", playerNum];
    [allPlayers setObject:playerInfo forKey:key];
}

现在,保存并阅读后allPlayers字典中,您将按如下方式访问单个记录:

NSDictionary * player2Info = [readDict objectForKey:@"player2"];
NSLog(@"%@", player2Info); // show all keys and values for player 2
NSLog(@"%@", [player2Info valueForKey:@"currentGame"]; // show a single value

第三点:如果您要存储多个用户的游戏状态,proper做到这一点的方法(从面向对象的角度来看)是创建一个Player类,然后加载并保存该类的实例。乍一看,它看起来像这样:

@interface Player {
    NSString * currentGame;
    NSString * currentGameType;
    NSString * currentGameQuestion;
    ...
}
@property (nonatomic, copy) NSString * currentGame;
@property (nonatomic, copy) NSString * currentGameType;
@property (nonatomic, copy) NSString * currentGameQuestion;
...
- (void)loadFromDictionary:(NSDictionary *)dict;
- (NSDictionary *)saveAsDictionary;
@end

@implementation Player
@synthesize currentGame;
@synthesize currentGameType;
@synthesize currentGameQuestion;
...

- (void)loadFromDictionary:(NSDictionary *)dict {
    [self setCurrentGame:[dict objectForKey:@"currentGame"]];
    [self setCurrentGameType:[dict objectForKey:@"currentGameType"]];
    [self setCurrentGameQuestion:[dict objectForKey:@"currentGameQuestion"]];
    ...
}

- (NSDictionary *)saveAsDictionary {
    return [NSDictionary dictionaryWithObjectsAndKeys:
        currentGame, @"currentGame";
        currentGameType, @"currentGameType";
        currentGameQuestion, @"currentGameQuestion";
        ...
    nil];
}

@end

现在,您可以按如下方式加载和保存玩家:

Player * player1 = [[[Player alloc] init] autorelease];
Player * player2 = [[[Player alloc] init] autorelease];
...

NSDictionary * players = [NSDictionary dictionaryWithObjectsAndKeys:
    [player1 saveAsDictionary], @"Player1",
    [player2 saveAsDictionary], @"Player2",
    ...
    nil];

// save dictionary
[players writeToFile:...

// load dictionary
NSDictionary * readDict = ...

[player1 loadFromDictionary:[readDict objectForKey:@"Player1"]];
[player2 loadFromDictionary:[readDict objectForKey:@"Player2"]];
...

See also:

Objective-C 中的嵌套数组 ( NSMutableArray ) https://stackoverflow.com/q/1244833/33686(玩家对象)
存储和检索多维 NSMutableArray 的最佳方法是什么? https://stackoverflow.com/q/2136905/33686(属性列表序列化)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NSDictionary读取数据 的相关文章

  • Firebase 查询 - 查找包含字符串的子项

    我在使用 Firebase 查询时遇到了一些问题 我想查询对象 其中对象子值包含特定字符串 到目前为止 我有一些看起来像这样的东西 Firebase ref Firebase alloc initWithUrl https dinosaur
  • 如果没有解析器生成器,如何用 C 或 Objective-C 编写解析器?

    我正在尝试用 C 或 Objective C 制作一个计算器 它接受以下字符串 8 2 4 3 9 2 并返回答案 2920 我不想使用像 Lex 或 Yacc 这样的生成器 所以我想从头开始编码 我该怎么做呢 除了 龙 这本书之外 还有其
  • Xcode - 重命名项目会导致问题

    我目前正在开发 iPhone 应用程序 我从在互联网上找到的项目模板开始研究这个问题 现在我想重命名这个项目 我已经成功重命名了实际的项目文件 文件夹和可执行文件以及其他一些东西 但有一个奇怪的问题 如果我尝试在 iPhone 设备上运行该
  • 如何在代码中的UIToolBar中添加UIBarButtonItem

    我有标准 UIBarButtonItem UIBarButtonItem share UIBarButtonItem alloc initWithBarButtonSystemItem UIBarButtonSystemItemAction
  • iPhone – 类似 Photoshop 的效果

    我可以在 iPhone 中制作乘法 屏幕 颜色或其他类似 Photoshop 的效果吗 Check 石英演示 http developer apple com iphone library samplecode QuartzDemo ind
  • 如何在当前视图上方创建半透明视图层?

    您可能以前见过这种情况 它在 ScoutMob 等消费时尚应用程序中变得非常流行 我正在尝试在启动时实现 60 的透明视图 该视图将覆盖我的主屏幕 解释如何使用主屏幕的功能并在点击时消失 我已经完成了整个应用程序 它从几年前就开始使用 xi
  • 使用 Xcode 资产目录缓存 UIImage

    我们都知道UIImage神秘的幕后缓存机制imageNamed 方法 在苹果的UIImage 类参考 https developer apple com library IOS documentation UIKit Reference U
  • 何时使用 takeUnretainedValue() 或 takeRetainedValue() 来检索 Swift 中的非托管对象?

    根据将 Swift 与 Cocoa 和 Objective C 结合使用 https developer apple com library prerelease ios documentation Swift Conceptual Bui
  • “UITableViewCell 附件复选标记”是图像吗?

    我需要定义一个自定义UITableViewCell哪里的UITableViewCellAccessoryCheckmark位于 a 的左侧UILabel 我应该将其定义为图像还是有更聪明的方法 非常感谢 卡洛斯 这只是一个关于苹果文档 ht
  • Objective C - 如何从 NSAttributedString 创建 rtf

    我可以使用以下方法从 rtf 字符串转换为属性字符串 NSAttributedString attributedStr NSAttributedString alloc initWithData data options NSDocumen
  • 以编程方式在 UIView 中添加 UIScrollView

    我有一个问题如何在UIView中添加UIScrollView 以便UIScrollView能够正常工作 UIScrollView scroll UIScrollView alloc initWithFrame CGRectMake 0 0
  • [NSDictionary getObjects:andKeys:] 的示例

    我找不到该方法的工作示例 NSDictionary getObjects andKeys 唯一的example http www mail archive com cocoa dev lists apple com msg24959 htm
  • UIAlertView 可以通过委托传递字符串和 int 吗

    我有一个 UIAlertView 事实上有几个 并且我正在使用该方法 void alertView UIAlertView alertView clickedButtonAtIndex NSInteger buttonIndex如果用户未按
  • 网络扩展 - NEVPNManager

    苹果在 iOS 8 中发布了一个新的框架 NetworkExtension 我想使用 NEVPNManager 从应用程序启动 VPN 连接 或者此框架还有其他用途吗 有人有关于这个框架的信息或例子吗 我在developer apple c
  • 如何在没有 IDP 会员资格的情况下直接将 iPhone 应用程序部署/调试到 iPhone 设备?

    你能告诉我如何使用 xcode 部署 调试 iphone 应用程序 但无需支付 99 美元 我这样做是为了学习目的 我看过一个指南 可以帮助将 iphone 应用程序部署到 iPhone 设备 例如 http www vinodlive c
  • 使用 OpenGL ES 绘制地球仪

    我正在尝试渲染一个地球仪 上面有地图的球体 OpenGL ES 1 1 on iOS 我能够绘制球体并绘制边界 但有一个问题 在我看来 不面向前方的线条也被绘制在屏幕上 像这样 在图片中 您可以看到美国渲染得很好 但您可以看到澳大利亚渲染在
  • EKCalendarChooser 多选不起作用

    我正在尝试使用EKCalendarChooser获取用户选择的多个日历 我是这样表达观点的 EKCalendarChooser dvc EKCalendarChooser alloc initWithSelectionStyle EKCal
  • 经常访问 NSUserDefaults

    在我的应用程序的逻辑处理过程中 我需要频繁访问用户首选项 并多次访问 10 15 次 以确定需要处理什么以及如何处理 也许这个问题不是关于性能的问题 而是关于正确执行的问题 目前我正在做一个 NSUserDefaults standardU
  • 从 JSON 在 Devise 中创建用户

    我正在致力于将运行 Devise 的 Rails 3 1 应用程序与我的 iOS 应用程序集成以进行用户身份验证 我希望用户能够从应用程序注册 然后我可以存储这些凭据以便稍后登录 使用 RestKit 我这样做 IBAction regis
  • 检测 UICollectionView 中的页面更改

    我尝试寻找这个问题一段时间 但找不到这个问题的答案 我的问题是我有一个UICollectionView滚动方向是Horizontal with Paging Enabled 我的问题是我想保留用户所在的当前页码 所以我创建了一个int变量

随机推荐