1

我有一个实体类,它继承了保存一些数据的 NSObject,类名是“MessageDetails”。IE

MessageDetails.h 类-

#import <Foundation/Foundation.h>

@interface MessageDetails : NSObject

@property (nonatomic, retain) NSString *userImage;
@property (nonatomic, retain) NSString *userName;
@property (nonatomic, retain) NSString *message;
@property (nonatomic, retain) NSString *dateString;

@end

MessageDetails.m 类-

#import "MessageDetails.h"

@implementation MessageDetails
@synthesize userImage, userName, message, dateString;

- (id)initWithCoder:(NSCoder *)decoder {
    if((self = [super init])) {
        self.userImage = [decoder decodeObjectForKey:@"userImage"];
        self.userName = [decoder decodeObjectForKey:@"userName"];
        self.message = [decoder decodeObjectForKey:@"message"];
        self.dateString = [decoder decodeObjectForKey:@"dateString"];
    }
    return self;
}

- (void)encodeWithCoder: (NSCoder *)coder
{
    [coder encodeObject:userImage forKey:@"userImage"];
    [coder encodeObject:userName forKey:@"userName"];
    [coder encodeObject:message forKey:@"message"];
    [coder encodeObject:dateString forKey:@"dateString"];
}

@end

在我的 View Controller 类中,我正在向Recipent 发送一些消息。成功发送消息后,我将一些值存储到“MessageDetails”类对象中,例如 userImage、userName、message 和 dateString,我必须在另一个类表视图中显示这些值。所以我使用此代码使用 NSUserDefault 成功设置了 messageDetails 对象值。

     NSMutableArray *finalArray=[[NSMutableArray alloc]init];
     NSMutableArray *array=[[NSMutableArray alloc]init];
     array= [Util getArrayPreference:@"MessageDetails"];
     if (array) {
             [finalArray addObjectsFromArray:array];
           }

     [finalArray addObject:messageDetails];
     [Util setArrayPreference:finalArray forKey:@"MessageDetails"];

//用于设置首选项

+(void)setPreference:(MessageDetails *)product forKey:(NSString *)key
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:product];
    [prefs setObject:myEncodedObject forKey:key];
    [prefs synchronize];
}

+(void)setArrayPreference:(NSMutableArray *)products forKey:(NSString *)string
{
    for (int i=0; i<products.count; i++) {
        MessageDetails *po=[products objectAtIndex:i];
        NSString *st=[NSString stringWithFormat:@"%d",i];
        [Util setPreference:po forKey:st];

    }
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:products];
    [prefs setObject:myEncodedObject forKey:string];
    [prefs synchronize];
}

//获取偏好

+(MessageDetails *)getPreference:(NSString *)key
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *myEncodedObject1 = [defaults objectForKey:key];
    MessageDetails *obj = (MessageDetails *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject1];
    return obj;
}

+(NSMutableArray *)getArrayPreference:(NSString *)string
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *myEncodedObject1 = [defaults objectForKey:string];
    NSMutableArray *arr1=[[NSMutableArray alloc]init];
    NSMutableArray *arr=(NSMutableArray *) [NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject1];
    for (int i=0;i<arr.count;i++) {
        NSString *st=[NSString stringWithFormat:@"%d",i];
        [arr1 addObject:[Util getPreference:st]];
    }
    return arr;
}

但我的问题是当我调用 getArrayPreference 方法之后

NSMutableArray *arr=(NSMutableArray *) [NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject1];

这条线崩溃了。我得到的一个例外是

2013-08-25 12:32:22.802 SmsIndia[551:1d003] -[MessageDetails initWithCoder:]:无法识别的选择器发送到实例 0xae68160 ****

2013-08-25 12:32:22.802 SmsIndia[551:1d003] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[MessageDetails initWithCoder:]:无法识别的选择器发送到实例 0xae68160” **

我做错了什么,但我不确定是什么。任何帮助将不胜感激......,谢谢!如果有人对问题有任何疑问,请告诉我。

4

0 回答 0