异常 - 自定义类initWithCoder无法识别的选择器发送到iPhone中的实例

时间:2013-08-25 07:26:49

标签: ios iphone nsuserdefaults nscoding

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

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 < / EM> ****

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

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

0 个答案:

没有答案