J'ai deux problèmes, j'ai une méthode qui construit un dictionnaire, puis je l'enregistre dans une variable globale que j'ai synthétisée. Si je l'assigne juste, quand j'essaye d'y accéder d'une autre méthode, son vide, si j'emploie la copie, il fuit la mémoire.iOS NSDictionary Copier la fuite de mémoire
Je peux juste l'assigner et cela fonctionne si c'est, disons, un objet "plus simple" comme un NSString, pourquoi cela ne fonctionne-t-il pas avec NSDictionary ?.
.h:
@interface ClassIHate : UIViewController{
NSDictionary *postBuild;
}
@property (nonatomic, retain) NSDictionary *postBuild;
-(void)prepData;
@end
je suis seulement, y compris les classes qui utilisent la Postbuild variable, comme c'est ma question. .m
@implementation ClassIHate
@synthesize postBuild;
- (void)viewDidUnload {
postBuild = nil;
}
- (void)dealloc {
[postBuild release];
[super dealloc];
}
-(void)prepData{
NSInteger i = 0;
NSMutableDictionary *_postBuild = [[NSMutableDictionary alloc]initWithCapacity:0];
for (NSString *key in self.keys) {
NSMutableArray *array = [ops valueForKey:key];
NSInteger j = 0;
for (MyDataType *object in array) {
NSString *abc = object.abc;
UITableViewCell *cell = [table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:i]];
UITextView *tv = (UITextView *)cell.accessoryView;
NSString *mon = tv.text;
NSString *monFormat = [[NSString stringWithFormat:@"%.2lf",[mon doubleValue]]stringByReplacingOccurrencesOfString:@"." withString:@","];
[_postBuild setObject:monFormat forKey:abc];
j++;
}
i++;
}
//postBuild = _postBuild; //Empty when called in other method
postBuild = [_postBuild copy]; //Leaks memory
[_postBuild release];
}
-(void)realizarOperaciones{
//DO STUFF
NSArray *postKeys = [postBuild allKeys]; //postBuild is nil if I dont use copy, leaks memory if I do.
//DO STUFF
}
Quel pourrait être le problème ici? Merci, Stefano.
C'est une variable d'instance, pas une variable globale. Ce n'est pas non plus une variable synthétisée; Seules les définitions de méthode sont synthétisées dans votre exemple. –