Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTPersistanceRecord setValue:forKey:报错 #31

Closed
lumouren009 opened this issue Jun 8, 2016 · 4 comments
Closed

CTPersistanceRecord setValue:forKey:报错 #31

lumouren009 opened this issue Jun 8, 2016 · 4 comments

Comments

@lumouren009
Copy link

lumouren009 commented Jun 8, 2016

- (BOOL)setPersistanceValue:(id)value forKey:(NSString *)key
{
    BOOL result = YES;
    NSString *setter = [NSString stringWithFormat:@"set%@%@:", [[key substringToIndex:1] capitalizedString], [key substringFromIndex:1]];
    if ([self respondsToSelector:NSSelectorFromString(setter)]) {
        if ([value isKindOfClass:[NSString class]]) {
            [self setValue:[value safeSQLDecode] forKey:key];
        } else if ([value isKindOfClass:[NSNull class]]) {
            [self setValue:nil forKey:key];
        } else {
            [self setValue:value forKey:key];
        }
    } else {
        result = NO;
    }

    return result;
}

在判断 [value isKindOfClass:[NSNull class]]分支里,value被设置为nil,导致crash

could not set nil as the value for the key 

情景:
新建migratorStep,实现

- (void)goUpWithQueryCommand:(CTPersistanceQueryCommand *)queryCommand error:(NSError *__autoreleasing *)error
{
  [[queryCommand addColumn:@"uploadFinishDate" columnInfo:@"INTEGER" tableName:@"LocalMovieTable"] executeWithError:error];
}

数据库中增加一个INTEGER类型的字段。数据库升级之后,由于新添字段,所以会走[value isKindOfClass:[NSNull class]]的分支,然而,这里uploadFinishDate并不是id类型,导致出错。

@lumouren009
Copy link
Author

这里我暂时的解决办法是把uploadFinishDate改成NSNumber类型

@casatwy
Copy link
Owner

casatwy commented Jun 8, 2016

你原来的uploadFinishDate是什么类型?

发自我的 iPhone

@casatwy
Copy link
Owner

casatwy commented Jun 8, 2016

如果是数字的内容,应该永远都使用NSNumber

@casatwy casatwy closed this as completed Jun 8, 2016
@lumouren009
Copy link
Author

原来的uploadFinishDate是unsigned long 类型

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants