开发者

NSDictionary loses data?

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];
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜