数组和字典兼容性
在 1.0 版本中,我使用 NSMutableArray,在其中存储 NSNumber,例如
NSMutableArray *myArray;
for(int i= 0 ; i < 10 ; i++)
[myArray addObject:[NSNumber numberWithInt:i]];
用于存储我的游戏状态。
在下一个版本(1.1)中,我想更新 myArray (通过在其中存储字典)...
NSMutableArray *myArray;
for(int i= 0 ; i < 10 ; i++)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:number forKey:@"index"];
[dict setValue:[NSNumber numberWithBool:NO] forKey:@"isValid"];
[myArray addObject:dict];
[dict release];
}
我的问题是如何为以前的版本 1.0 提供兼容性。
提前致谢。
更新:我支持 1.1 版本,如下所示,可以吗。
//Version 1.1 support
//Read myArray
if( [myArray count] > 0)
{
if( ![[myArray objectAtIndex:0] isKindOfClass:[NSMutableDictionary class]])
{
for( NSNumber *number in myArray)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:number forKey:@"index"];
[dict setValue:[NSNumber numberWithBool:NO] forKey:@"isValid"];
[dict release];
}
}
}
In version 1.0 i am using NSMutableArray and inside that i am storing NSNumbers like
NSMutableArray *myArray;
for(int i= 0 ; i < 10 ; i++)
[myArray addObject:[NSNumber numberWithInt:i]];
For storing my gameStatus.
In Next version(1.1) I want to update myArray like(by storing dictionary inside it)...
NSMutableArray *myArray;
for(int i= 0 ; i < 10 ; i++)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:number forKey:@"index"];
[dict setValue:[NSNumber numberWithBool:NO] forKey:@"isValid"];
[myArray addObject:dict];
[dict release];
}
My problem is how to provide compatibility for previous version 1.0.
Thanks in advance.
Updated : I am supporting Version 1.1 as shown below, is it ok.
//Version 1.1 support
//Read myArray
if( [myArray count] > 0)
{
if( ![[myArray objectAtIndex:0] isKindOfClass:[NSMutableDictionary class]])
{
for( NSNumber *number in myArray)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:number forKey:@"index"];
[dict setValue:[NSNumber numberWithBool:NO] forKey:@"isValid"];
[dict release];
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的观点是您在某个时候将它们保存到磁盘,并且您希望在升级后处理加载它们。有很多方法可以处理这个问题,但对于这种特定情况,我建议采用以下方法:
当您到达 v1.2 并再次更改数据时,这可以很好地扩展。您将需要或多或少地永远保留“如何阅读 v1.0”代码(或者至少直到您完全放弃从 1.0 升级的支持),但无论如何都是如此。
I assume your point is that you're saving these to disk at some point, and you want to handle loading them after upgrade. There are tons of ways to handle this, but here's what I would recommend for this specific case:
This scales well when you get to v1.2 and change your data again. You will need to keep around your "how to read v1.0" code more or less forever (or at least until you completely drop support for upgrading from 1.0), but that's true in any case.