简体   繁体   English

NSUserDefaults的内存问题

[英]Memory issues with NSUserDefaults

I have spent hours trying to get my project working and I just can't get it working. 我花了几个小时试图使我的项目正常工作,但我却无法使其正常工作。

Basically, I'm trying to use NSUserDefaults to save a custom object when the user hits a save button and load all the data up when the app loads. 基本上,我试图使用NSUserDefaults在用户单击保存按钮时保存自定义对象,并在应用程序加载时加载所有数据。 If there is no previous NSUserDefault saved, I want to set some defaults. 如果没有保存以前的NSUserDefault,我想设置一些默认值。 In the end, I am getting EXC_BAD_ACCESS when trying to load a previously-saved NSUserDefault. 最后,尝试加载先前保存的NSUserDefault时,我得到EXC_BAD_ACCESS。 It works fine the first load, when setting the starting data. 设置起始数据时,它可以很好地完成第一次加载。 And the thing is, when I try to enable NSZombieEnabled and the other env vars for it, it somehow loads fine without the EXC_BAD_ACCESS. 事实是,当我尝试为其启用NSZombieEnabled和其他环境变量时,它在某种程度上无需使用EXC_BAD_ACCESS就可以很好地加载。 So here's what I'm working with: 因此,这就是我正在处理的内容:

[App Delegate.h] [App Delegate.h]

#import <UIKit/UIKit.h>
#import "Note.h"

@interface ToDoWallAppDelegate : NSObject <UIApplicationDelegate> {
    ...
    Note *note;
}

...
@property (retain) Note *note;

@end

[App Delegate.m] [App Delegate.m]

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    ...
    note = [[Note alloc] init];

    NSUserDefaults *stdDefaults = [NSUserDefaults standardUserDefaults];
    NSData *noteData = [stdDefaults objectForKey:@"Note"];
    if (noteData) {
        self.note = (Note *)[NSKeyedUnarchiver unarchiveObjectWithData:noteData];
    } else {
        note.background = [UIImage imageNamed:@"Cork.jpg"];
        note.picture = [UIImage imageNamed:@"Cork.jpg"];
        note.font = [UIFont fontWithName:@"Helvetica" size:18.0f];
        note.fontColor = [UIColor blackColor];
        note.fontNameIndex = 9;
        note.fontSizeIndex = 6;
        note.fontColorIndex = 0;
        note.backgroundIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        note.pictureIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        note.text = @"Type note here...";
    }

    ...
}

- (void)dealloc {
    ...
    [note release];
    [super dealloc];
}

[View Controller] [View Controller]

- (void)saveNote {
    ...
    NSUserDefaults *stdDefaults = [NSUserDefaults standardUserDefaults];
    if (stdDefaults) {
        NSData *noteData = [NSKeyedArchiver archivedDataWithRootObject:UIAppDelegate.note];
        [stdDefaults setObject:noteData forKey:@"Note"];
        [stdDefaults synchronize];
    }
}

[Note.h] [Note.h]

#import <Foundation/Foundation.h>


@interface Note : NSObject <NSCoding> {
    UIImage *background, *picture;
    UIFont *font;
    UIColor *fontColor;
    int fontNameIndex, fontSizeIndex, fontColorIndex;
    NSIndexPath *backgroundIndexPath, *pictureIndexPath;
    BOOL customBackground;
    NSString *text;
}

@property (retain) UIImage *background, *picture;
@property (retain) UIFont *font;
@property (retain) UIColor *fontColor;
@property int fontNameIndex, fontSizeIndex, fontColorIndex;
@property (retain) NSIndexPath *backgroundIndexPath, *pictureIndexPath;
@property BOOL customBackground;
@property (retain) NSString *text;

- (Note *)init;

@end

[Note.m] [注意.m]

#import "Note.h"


@implementation Note

@synthesize background, picture, font, fontColor, fontNameIndex, fontSizeIndex, fontColorIndex, customBackground, backgroundIndexPath, pictureIndexPath, text;

- (Note *)init {
    if (self = [super init]) {
        background = [[UIImage alloc] init];
        picture = [[UIImage alloc] init];
        font = [[UIFont alloc] init];
        fontColor = [[UIColor alloc] init];
        backgroundIndexPath = [[NSIndexPath alloc] init];
        pictureIndexPath = [[NSIndexPath alloc] init];
        text = [[NSString alloc] init];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    NSData *dataBackground = UIImagePNGRepresentation(background);
    NSData *dataPicture = UIImagePNGRepresentation(picture);

    [encoder encodeObject:dataBackground forKey:@"dataBackground"];
    [encoder encodeObject:dataPicture forKey:@"dataPicture"];
    [encoder encodeObject:font forKey:@"font"];
    [encoder encodeObject:fontColor forKey:@"fontColor"];
    [encoder encodeInt:fontSizeIndex forKey:@"fontSizeIndex"];
    [encoder encodeInt:fontColorIndex forKey:@"fontColorIndex"];
    [encoder encodeBool:customBackground forKey:@"customBackground"];
    [encoder encodeObject:backgroundIndexPath forKey:@"backgroundIndexPath"];
    [encoder encodeObject:pictureIndexPath forKey:@"pictureIndexPath"];
    [encoder encodeObject:text forKey:@"text"];
}

- (Note *)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        NSData *dataBackground = [decoder decodeObjectForKey:@"dataBackground"];
        NSData *dataPicture = [decoder decodeObjectForKey:@"dataPicture"];
        background = [[UIImage imageWithData:dataBackground] retain];
        picture = [[UIImage imageWithData:dataPicture] retain];
        font = [[decoder decodeObjectForKey:@"font"] retain];
        fontColor = [[decoder decodeObjectForKey:@"fontColor"] retain];
        fontNameIndex = [decoder decodeIntForKey:@"fontNameIndex"];
        fontColorIndex = [decoder decodeIntForKey:@"fontColorIndex"];
        customBackground = [decoder decodeBoolForKey:@"customBackground"];


    backgroundIndexPath = [[decoder decodeObjectForKey:@"backgroundIndexPath"] retain];
        text = [[decoder decodeObjectForKey:@"text"] retain];
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
    [background release];
    [picture release];
    [font release];
    [fontColor release];
    [backgroundIndexPath release];
    [pictureIndexPath release];
    [text release];
}

@end

I really need some help I appreciate it. 我真的需要一些帮助,我很感激。

Edit: 编辑:

Btw, there are also lines from other files that edit the App Delegate's Note object, such as: 顺便说一句,还有其他文件中的行可以编辑App Delegate的Note对象,例如:

#define UIAppDelegate ((ToDoWallAppDelegate *)[UIApplication sharedApplication].delegate)
...
UIAppDelegate.note.backgroundIndexPath = indexPath;

Edit: 编辑:

This is what debugger wrote: 这是调试器写的:

#0  0x90be9ed7 in objc_msgSend
#1  0x03b05210 in ??
#2  0x000023ce in -[ToDoWallAppDelegate setNote:] at ToDoWallAppDelegate.m:14
#3  0x00002216 in -[ToDoWallAppDelegate applicationDidFinishLaunching:] at ToDoWallAppDelegate.m:35

Which are: 哪个是:

note.text = @"Type note here...";
//and
@synthesize window, note;

我不确定这是否是造成您问题的原因,但我认为[super dealloc]应该是您的dealloc方法的最后一行,而不是第一行。

Have a look at documentation how to setup defaults Using NSUserDefaults 看一下文档如何使用NSUserDefaults设置默认值

If you need more info have a look in Hillega's book "Cocoa programming for Mac OS X" bignerdranch.com/books, it's explained there. 如果需要更多信息,请参阅Hillega的书“ Mac OS X的可可编程” bignerdranch.com/books,在那里进行了解释。

You should consider changing the headline to "How to use NSUserDefaults" ... 您应该考虑将标题更改为“如何使用NSUserDefaults” ...

Example how to setup default value, in your class in initialize put something like: 示例如何设置默认值,请在您的类中initialize

+ (void)initialize{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *appDefaults = [NSDictionary
        dictionaryWithObject:@"YES" forKey:@"DeleteBackup"];

    [defaults registerDefaults:appDefaults];
}

You could post where exactly are you getting the error instead of posting the whole code. 您可以发布错误的确切位置,而不是发布整个代码。 Run it through debugger and see where it stops. 通过调试器运行它,看看它在哪里停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM