NSDictionary 丢失数据?
我看过 NSDictionary 丢失数据的帖子,但就我而言,它有点奇怪。我有一个类,其中包含一些数据,包括 NSString 、 NSIntegers 和 GLfloats 。 (据我所知,所有这些都符合 NSCopying,不像 CGFloat 之类的东西。)
我在我的应用程序中快速访问文件并且一切正常,但随后我尝试再次访问它(以重新加载/刷新)屏幕,然后NSInteger 值返回 180000 和几十亿之间的值,我知道这个值肯定是 4,所以由于某种原因数据没有持久化。
感觉数据在某个时刻正在丢失/发布/更改,但我不知道怎么可能。
编辑:
这是一些代码,这是创建和存储texturedQuads的地方,为我在init方法中拥有的每个图集调用该方法
此外,我还更改了滥用单词包装器的位置
-(void)loadAtlasData:(NSString *)atlasName
{
NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
if (quadLibrary == nil)
quadLibrary = [[NSMutableDictionary alloc] init];
CGSize atlasSize = [self loadTextureImage:[atlasName
stringByAppendingPathExtension:@"png"]
materialKey:atlasName];
NSArray *itemData = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:atlasName ofType:@"plist"]];
for(NSDictionary *record in itemData)
{
TexturedQuad *quad = [self texturedQuadFromAtlasRecord:record
atlasSize:atlasSize
materialKey:atlasName];
[quadLibrary setObject:quad forKey:
[record objectForKey:@"name"]];
}
[apool release];
}
-(TexturedQuad *)texturedQuadFromAtlasRecord:(NSDictionary *)record
atlasSize:(CGSize)atlasSize
materialKey:(NSString *)key
{
TexturedQuad *quad = [[TexturedQuad alloc] init];
GLfloat xLocation = [[record objectForKey:@"xLocation"] floatValue];
GLfloat yLocation = [[record objectForKey:@"yLocation"] floatValue];
GLfloat width = [[record objectForKey:@"width"] floatValue];
GLfloat height = [[record objectForKey:@"height"] floatValue];
//find the normalized texture co-ordinates
GLfloat uMin = xLocation/atlasSize.width;
GLfloat vMin = yLocation/atlasSize.height;
GLfloat uMax = (xLocation + width)/atlasSize.width;
GLfloat vMax = (yLocation + height)/atlasSize.height;
quad.uvCoordinates[0] = uMin;
quad.uvCoordinates[1] = vMax;
quad.uvCoordinates[2] = uMax;
quad.uvCoordinates[3] = vMax;
quad.uvCoordinates[4] = uMin;
quad.uvCoordinates[5] = vMin;
quad.uvCoordinates[6] = uMax;
quad.uvCoordinates[7] = vMin;
quad.materialKey = key;
return [quad autorelease];
}
第二次编辑:
添加了plist的示例文件
dict
(key)name
(string)sync
(key)xLocation
(integer)489
(key)yLocation
(integer)36
(key)width
(integer)21
(key)height
(integer)21
/dict
本质上是一个数组,由字典组成,每个字典保存图集中图片的数据。所以有名称、xLocation、yLocation、宽度、高度。
编辑3: 这是我加载对象的位置 我使用 [MaterialController sharedMaterialController] 来获取此控制器的实例
-(TexturedQuad *)quadFromAtlasKey:(NSString *)atlasKey
{
return [quadLibrary objectForKey:atlasKey];
}
I've seen posts where NSDictionary loses data but in my case its a little wierd. I have a class that includes some data including NSString and NSIntegers and GLfloats. (As far as I know all of these do conform to NSCopying unlike things like CGFloat.)
I access the files quickly in my app and it all works, but then I try to access it again (to reload/refresh) the screen and then the NSInteger values return something between 180000, and a few billion for a value I know is definitely 4 so for some reason the data hasn't persisted.
It feels like the data is being lost/released/changed at some point but I don't know how it possibly can.
Edit:
Heres some of the code, this is where the texturedQuads are created and store, the method is called for each atlas I have in the init method
Also I have changed where I misused the word wrapper
-(void)loadAtlasData:(NSString *)atlasName
{
NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
if (quadLibrary == nil)
quadLibrary = [[NSMutableDictionary alloc] init];
CGSize atlasSize = [self loadTextureImage:[atlasName
stringByAppendingPathExtension:@"png"]
materialKey:atlasName];
NSArray *itemData = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:atlasName ofType:@"plist"]];
for(NSDictionary *record in itemData)
{
TexturedQuad *quad = [self texturedQuadFromAtlasRecord:record
atlasSize:atlasSize
materialKey:atlasName];
[quadLibrary setObject:quad forKey:
[record objectForKey:@"name"]];
}
[apool release];
}
-(TexturedQuad *)texturedQuadFromAtlasRecord:(NSDictionary *)record
atlasSize:(CGSize)atlasSize
materialKey:(NSString *)key
{
TexturedQuad *quad = [[TexturedQuad alloc] init];
GLfloat xLocation = [[record objectForKey:@"xLocation"] floatValue];
GLfloat yLocation = [[record objectForKey:@"yLocation"] floatValue];
GLfloat width = [[record objectForKey:@"width"] floatValue];
GLfloat height = [[record objectForKey:@"height"] floatValue];
//find the normalized texture co-ordinates
GLfloat uMin = xLocation/atlasSize.width;
GLfloat vMin = yLocation/atlasSize.height;
GLfloat uMax = (xLocation + width)/atlasSize.width;
GLfloat vMax = (yLocation + height)/atlasSize.height;
quad.uvCoordinates[0] = uMin;
quad.uvCoordinates[1] = vMax;
quad.uvCoordinates[2] = uMax;
quad.uvCoordinates[3] = vMax;
quad.uvCoordinates[4] = uMin;
quad.uvCoordinates[5] = vMin;
quad.uvCoordinates[6] = uMax;
quad.uvCoordinates[7] = vMin;
quad.materialKey = key;
return [quad autorelease];
}
Second edit:
Added an example of the plist file
dict
(key)name
(string)sync
(key)xLocation
(integer)489
(key)yLocation
(integer)36
(key)width
(integer)21
(key)height
(integer)21
/dict
essentially its an array, consisting of dictionaries, each dictionary holds the data for the picture in the atlas. so theres the name, xLocation, yLocation, width, height.
edit 3:
Here is where I load the object from
I use a [MaterialController sharedMaterialController] to get an instance of this controller
-(TexturedQuad *)quadFromAtlasKey:(NSString *)atlasKey
{
return [quadLibrary objectForKey:atlasKey];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看删除自动释放池是否可以解决问题。据我所知,您不需要它,因为 quadLibrary 似乎是一个 iVar,而 itemData 由其父类自动释放,所有 >您在for循环中返回的>TextureQuads。
See if removing the auto-release pool sorts the issue. As far as I can see you don't need it as quadLibrary seems to be an iVar whilst itemData is auto-released by its parent class, as are all the TextureQuads you return in your for loop.