Objective c 带代码的内存管理

Objective c 带代码的内存管理,objective-c,cocos2d-iphone,Objective C,Cocos2d Iphone,嗨,我有两个类,一个是plisreader,它是下面的代码 // // PlistReader.h // NationalAntemsAndFlags // // Created by mac on 12/17/11. // Copyright (c) 2011 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> #import "CountryClass.h" @int

嗨,我有两个类,一个是plisreader,它是下面的代码

//
//  PlistReader.h
//  NationalAntemsAndFlags
//
//  Created by mac on 12/17/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "CountryClass.h"

@interface PlistReader : NSObject
{
    NSDictionary *temp;
}
@property (nonatomic,retain) NSDictionary *temp;

-(int)LengthOfPList;
-(id)GetTheObjectById:(NSString*)ObjId;
-(void)initWithFileName:(NSString*)fileName;
- (id)getCountryInfoById:(int)id;
@end
//
//  CountryClass.h
//  NationalAntemsAndFlags
//
//  Created by mac on 12/17/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CountryClass : NSObject
{
    NSString *Name ;
    NSString *ImageUrl;
    NSString *AnthemUrl;
    NSString *ShortDetail;
    NSString *completeDetails;
    int LocationX ;
    int LocationY ;


}
@property (nonatomic,retain) IBOutlet NSString *Name;
@property (nonatomic,retain) IBOutlet NSString *ImageUrl;
@property (nonatomic,retain) IBOutlet NSString *AnthemUrl;
@property (nonatomic,retain) IBOutlet NSString *ShortDetail;
@property (nonatomic,retain) IBOutlet NSString *completeDetails;
@property (nonatomic) IBOutlet int LocationY;
@property (nonatomic) IBOutlet int LocationX;



@end
另一个是country类,它有以下代码

//
//  PlistReader.h
//  NationalAntemsAndFlags
//
//  Created by mac on 12/17/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "CountryClass.h"

@interface PlistReader : NSObject
{
    NSDictionary *temp;
}
@property (nonatomic,retain) NSDictionary *temp;

-(int)LengthOfPList;
-(id)GetTheObjectById:(NSString*)ObjId;
-(void)initWithFileName:(NSString*)fileName;
- (id)getCountryInfoById:(int)id;
@end
//
//  CountryClass.h
//  NationalAntemsAndFlags
//
//  Created by mac on 12/17/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CountryClass : NSObject
{
    NSString *Name ;
    NSString *ImageUrl;
    NSString *AnthemUrl;
    NSString *ShortDetail;
    NSString *completeDetails;
    int LocationX ;
    int LocationY ;


}
@property (nonatomic,retain) IBOutlet NSString *Name;
@property (nonatomic,retain) IBOutlet NSString *ImageUrl;
@property (nonatomic,retain) IBOutlet NSString *AnthemUrl;
@property (nonatomic,retain) IBOutlet NSString *ShortDetail;
@property (nonatomic,retain) IBOutlet NSString *completeDetails;
@property (nonatomic) IBOutlet int LocationY;
@property (nonatomic) IBOutlet int LocationX;



@end
所以我在我的场景中这样叫它

-(void)LoadData:(int)countryId
{
    CId = countryId;
    PlistReader *pList =[[PlistReader alloc]init];
    [pList initWithFileName:@"CountryDetails"];   

    CountryClass *objcountry = (CountryClass*) [pList getCountryInfoById:countryId];

    NSString *tempLongDetails = objcountry.completeDetails;
    NSString *tempShortDetails = objcountry.ShortDetail;
    NSString *fileName =  objcountry.ImageUrl ;
    CCSprite *flag ;

    NSString * fullPath = [[NSBundle mainBundle] pathForResource: [fileName stringByDeletingPathExtension]
                                                             ofType: [fileName pathExtension]
                                                           inDirectory: @"CountryFlags"];


    NSLog(fullPath);
    if (fullPath)
    {
       UIImage  *theImage = [UIImage imageWithContentsOfFile: fullPath];
       if (theImage)
       {
            flag = [CCSprite spriteWithCGImage: [theImage CGImage] key: fileName];
           // flag = [CCSprite spriteWithFile:fileName];
            flag.position = ccp(200, 265);
           flag.scale = .255;
       }
    }



    TextViewTopFlagData = [[UITextView alloc]init];
    TextViewTopFlagData.text = tempShortDetails;
    TextViewTopFlagData.frame = CGRectMake(260,17, 105, 75);
    TextViewTopFlagData.backgroundColor = [UIColor clearColor];
    [TextViewTopFlagData setEditable:NO];


    TextViewDownFlagData = [[UITextView alloc]init];
    TextViewDownFlagData.text = tempLongDetails;
    TextViewDownFlagData.frame = CGRectMake(22,240, 242, 61);
    TextViewDownFlagData.backgroundColor = [UIColor clearColor];
    [TextViewDownFlagData setEditable:NO];
    [[[CCDirector sharedDirector]openGLView]addSubview:TextViewTopFlagData];
    [[[CCDirector sharedDirector]openGLView]addSubview:TextViewDownFlagData];
    [self addChild:flag];

}

@end

但是在函数末尾,objcountry将为空或不可引用。有人能解释一下为什么会发生这种情况吗?

只要浏览一下您的代码,我就会发现:

temp =(NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
您需要保留它,如下所示:

temp = [(NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc] retain];

这对您的bug没有帮助,但最好不要保留具有可变子类的属性(即NSString有NSMutableString,NSSet有NSMutableSet,NSDictionary有NSMutableDictionary)。最好(更安全)复制它们。i、 e

@property (nonatomic, retian) NSString *name; // worse
@property (nonatomic, copy) NSString *name; // better

有关原因,请阅读

此函数返回的是(id)而不是(CountryClass*)吗

您的问题是,在返回objCountry并将其引用到id对象之前,先释放它

    id ObjCountryInfo= objCountry;
    [objCountry release];
    return ObjCountryInfo;

我的建议是不要在这个方法中释放countryObject(使用完后再释放),并将返回类型更改为CountryClass*

请只包含实际引用您正在讨论的问题的代码,这太长了。我想人们可能不理解我想说的,为什么我把所有的代码都放在这里,主要问题是objcountry在函数末尾丢失了值或被破坏了,为什么会发生这种情况that@precious.logic您可以使用下面的“编辑”链接编辑此问题。不需要删除和转发。谢谢兄弟,这是很好的信息,我会试试,然后告诉你它是否有效,但谢谢
-(id)getCountryInfoById:(int)objId
{

    NSString *objsId = [NSString stringWithFormat:@"%d",objId];
    NSDictionary *CDirectory = (NSDictionary*) [self GetTheObjectById:objsId];
    int count = CDirectory.count;
    if(count>0)
    {
        CountryClass *objCountry = [[CountryClass alloc] init];   
        objCountry.Name= [CDirectory objectForKey:@"Name"];
        objCountry.LocationX =[[CDirectory objectForKey:@"PositionX"] intValue];
        objCountry.LocationY = [[CDirectory objectForKey:@"PositionY"] intValue];
        objCountry.ImageUrl = [CDirectory objectForKey:@"ImageUrl"];
        objCountry.AnthemUrl =[CDirectory objectForKey:@"AnthemUrl"];
        objCountry.ShortDetail =[CDirectory objectForKey:@"short Info"];
        objCountry.completeDetails = [CDirectory objectForKey:@"Details"];
        id ObjCountryInfo= objCountry;
        [objCountry release];
        return ObjCountryInfo;
    }
    return NULL;
}