我想将自定义类MCOIMAPSession对象存储到sqlite3数据库中。我阅读了有关NSKeyedArchiver的内容,并尝试如下使用。

- (void) updateImapSessionForAccount :(NSString *) emailAddress :(MCOIMAPSession *)imapSession {

    const char *dbpath = [databasePath UTF8String];

    //SQLIte Statement
    NSString *selettablequery = [NSString stringWithFormat:@"select * from MailBoxInfoDBTable"];

    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {

        //if(sqlite3_prepare_v2(database, [selettablequery UTF8String], -1, &statement, NULL) == SQLITE_OK)
        if (sqlite3_prepare(database, [selettablequery UTF8String], -1, &statement, NULL) ==SQLITE_OK)
        {
            // Take an array to store all string.
            //NSMutableArray *allRows = [[NSMutableArray alloc] init];

            while(sqlite3_step(statement) == SQLITE_ROW)
            {
                char *emailfield = (char *) sqlite3_column_text(statement,0);
                NSString *emailStr = [[NSString alloc] initWithUTF8String: emailfield];
                NSLog(@"updateMailMessagesPerAccount: %@", emailStr);

                if([emailStr isEqualToString:emailAddress])
                {
                    sqlite3_reset(statement);

                    NSData *messagesData = [NSKeyedArchiver archivedDataWithRootObject:imapSession];
                    NSString* stringFromData = [messagesData base64EncodedStringWithOptions:0];

                    NSString *sqlStr = [NSString stringWithFormat:@"update MailBoxInfoDBTable set imapsession='%@' where emailid='%@'", stringFromData, emailAddress];

                    const char *updateStatement = [sqlStr UTF8String];

                    if (sqlite3_prepare(database, updateStatement, -1, &statement, NULL) == SQLITE_OK)
                    {
                        if(SQLITE_DONE != sqlite3_step(statement))
                            NSLog(@"Error while updating. %s", sqlite3_errmsg(database));

                        sqlite3_finalize(statement);
                        sqlite3_close(database);

                        return;
                    }
                    else
                    {
                        NSLog(@"Error in statement: %s", sqlite3_errmsg(database));
                    }
                }
            }
        }
        sqlite3_finalize(statement);
        sqlite3_close(database);
    }

}

- (MCOIMAPSession *) retrieveImapSessionForAccount :(NSString *) emailAddress {

    MCOIMAPSession *imapsessionObj = nil;

    const char *dbpath = [databasePath UTF8String];

    //SQLIte Statement
    NSString *selettablequery = [NSString stringWithFormat:@"select * from MailBoxInfoDBTable"];

    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        //if(sqlite3_prepare_v2(database, [selettablequery UTF8String], -1, &statement, NULL) == SQLITE_OK)
        if (sqlite3_prepare(database, [selettablequery UTF8String], -1, &statement, NULL) ==SQLITE_OK)
        {
            // Take an array to store all string.
            //NSMutableArray *allRows = [[NSMutableArray alloc] init];

            while(sqlite3_step(statement) == SQLITE_ROW)
            {
                char *emailfield = (char *) sqlite3_column_text(statement, 0);
                NSString *emailStr = [[NSString alloc] initWithUTF8String: emailfield];
                NSLog(@"retrieveImapSessionForAccount: Email: %@", emailStr);

                if([emailStr isEqualToString:emailAddress])
                {
                    //const void *bytes = sqlite3_column_blob(statement, 3);
                    char *emailstring = (char *) sqlite3_column_text(statement, 3);
                    if (emailstring) {
                        NSString *messageStr = [[NSString alloc] initWithUTF8String: emailstring];
                        NSData *data = [[NSData alloc] initWithBase64EncodedString:messageStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
                        imapsessionObj = [NSKeyedUnarchiver unarchiveObjectWithData:data];
                    }
                }
            }
        }
        sqlite3_finalize(statement);
        sqlite3_close(database);
    }

    return imapsessionObj;
}


在执行encodeWithCoder unrecognized selector sent to instance时出现崩溃NSKeyedArchiver archivedDataWithRootObject:imapSession

然后,我在第三方类MCOIMAPSession.mm文件中添加了以下方法

- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self forKey:@"PROPERTY_KEY"];
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    if(self = [super init]){
        self = [aDecoder decodeObjectForKey:@"PROPERTY_KEY"];
    }
    return self;
}


更新:我也尝试了以下方法。

  @interface MCOIMAPSession : NSObject
    ....
  @end
  @interface NSObject (NSCoding)
     -(id)initWithCoder:(NSCoder*)decoder;
     -(void)encodeWithCoder:(NSCoder*)encoder;
  @end
  #endif

@implementation MCOIMAPSession
-(id)initWithCoder:(NSCoder *)decoder {
    if ((self=[super initWithCoder:decoder])) {
    }
    return self;
}
@end
@implementation NSObject (NSCoding)
-(id)initWithCoder:(NSCoder*)decoder {
    return [self init];
}
-(void)encodeWithCoder:(NSCoder*)encoder {}
@end


这是MCOIMAPSESSION MCOIMAPSESSION link的文件。.请让我知道我现在如何添加属性?

但是,我仍然看到相同的崩溃。当将自定义类MCOIMAPSession对象存储在sqlite数据库中时,有人可以纠正我在这里做错的事情吗?

最佳答案

您对NSCoding方法的实现不正确。您无需对self进行编码/解码,而是对self的每个属性/ ivar进行编码/解码。就像是:

- (void)encodeWithCoder:(NSCoder *)aCoder{
    // Replace the following with the class's actual properties
    [aCoder encodeObject:self.propertyA forKey:@"propertyA"];
    [aCoder encodeObject:self.propertyB forKey:@"propertyB"];
    [aCoder encodeObject:self.propertyC forKey:@"propertyC"];
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    if(self = [super init]){
        // Replace the following with the class's actual properties
        _propertyA = [aDecoder decodeObjectForKey:@"propertyA"];
        _propertyB = [aDecoder decodeObjectForKey:@"propertyB"];
        _propertyC = [aDecoder decodeObjectForKey:@"propertyC"];
    }
    return self;
}


顺便说一句-您的问题与SQLite完全无关。您的问题应仅局限于编码/解码问题。

09-16 10:47