2011-12-19 79 views
-3

喜内存管理我有2类之一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 

//实现文件

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

#import "PlistReader.h" 

@implementation PlistReader 
@synthesize temp; 


-(id)init 
{ 
    if(self =[super init]) 
    { 

    } 
    return self; 
} 
-(void)initWithFileName:(NSString*)fileName 
{ 
    // Data.plist code 
    // get paths from root direcory 

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
    // get documents path 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    // get the path to our Data/plist file 
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:[fileName stringByAppendingString:@"plist"]]; 

    // check to see if Data.plist exists in documents 
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) 
    { 
     // if not in documents, get property list from main bundle 
     plistPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"]; 
    } 

    // read property list into memory as an NSData object 
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; 
    NSString *errorDesc = nil; 
    NSPropertyListFormat format; 
    // convert static property liost into dictionary object 
    temp =(NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; 
    if (!temp) 
    { 
     NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); 
    } 
    // assign values 

} 

-(int)LengthOfPList 
{ 
    return temp.count; 
} 
-(id)GetTheObjectById:(NSString*)ObjId 
{ 

    return [temp objectForKey:ObjId]; 

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

-(void)dealloc 
{ 
    NSLog(@"dealloc plist"); 
    [temp release]; 
    [super dealloc]; 
} 




@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 

执行文件

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

#import "CountryClass.h" 

@implementation CountryClass 

@synthesize Name,ImageUrl,AnthemUrl,ShortDetail,completeDetails,LocationX,LocationY; 


@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年底将是空的或从参考数据任何一个可以解释我,这是为什么happaning

+4

请只包括实际引用您的问题代码正在谈论,这太长了。 – jrturton 2011-12-19 16:40:19

+0

我以为可能是人们无法理解我在说什么,为什么我把所有的代码放在这里主要问题是那个objcountry在函数的末尾丢了值或者被破坏了所以为什么会发生那个 – 2011-12-19 16:45:24

+0

@live.logic你可以使用它下面的“编辑”链接编辑这个问题。无需删除并重新发布。 – 2011-12-22 05:20:03

回答

0

刚虽然一眼你的代码,这种跳出我:

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

你需要保留它,就像这样:

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

这不会帮助你的错误,但它是很好的做法不保留有一个可变的子类的属性(即NSString有NSMutableString,NSSet有NSMutableSet,NSDictionary有NSMutableDictionary)。它更好(更安全),而不是copy。即

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

对于其中原因,读the accepted answer here

+0

感谢兄弟这是很好的信息,我会尝试一下,然后告诉你它是否工作,但谢谢 – 2011-12-19 18:01:58

0

是否有这个函数返回一个意思,而不是(CountryClass *)(ID)

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

你的问题是你释放objCountry返回并引用它到一个id对象。

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

我的建议是不要释放countryObject在这个方法中(释放它,当你用它做),并更改返回类型CountryClass *