Skip to content

Commit

Permalink
不混淆 string 的 @"" 跟 nil
Browse files Browse the repository at this point in the history
  • Loading branch information
ljh committed Aug 12, 2016
1 parent 32b9d70 commit 4dec54e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 27 deletions.
1 change: 0 additions & 1 deletion Demo-iOS/iOS-Demo/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ -(void)test

//外键 foreign key
LKTestForeign* foreign = [[LKTestForeign alloc]init];
foreign.address = @":asdasdasdsadasdsdas";
foreign.postcode = 123341;
foreign.addid = 213214;

Expand Down
11 changes: 7 additions & 4 deletions LKDBHelper/Helper/LKDBHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ - (void)fixSqlColumnsWithClass:(Class)clazz tableName:(NSString*)tableName
}

NSString* alertSQL = [NSString stringWithFormat:@"alter table %@ add column %@", tableName, addColumePars];
NSString* initColumnValue = [NSString stringWithFormat:@"update %@ set %@=%@", tableName, property.sqlColumnName, [property.sqlColumnType isEqualToString:LKSQL_Type_Text] ? @"''" : @"0"];
NSString* initColumnValue = [NSString stringWithFormat:@"update %@ set %@=%@", tableName, property.sqlColumnName, [property.sqlColumnType isEqualToString:LKSQL_Type_Text] ? @"null" : @"0"];

BOOL success = [db executeUpdate:alertSQL];

Expand Down Expand Up @@ -760,9 +760,9 @@ - (id)modelValueWithProperty:(LKDBProperty*)property model:(NSObject*)model
}

if (value == nil) {
value = @"";
value = [NSNull null];
}

return value;
}

Expand Down Expand Up @@ -1115,7 +1115,10 @@ - (NSMutableArray*)executeResult:(FMResultSet*)set Class:(Class)modelClass table
}
else {
NSData* sqlData = [set dataForColumnIndex:i];
NSString* sqlValue = [[NSString alloc] initWithData:sqlData encoding:NSUTF8StringEncoding];
NSString* sqlValue = nil;
if (sqlData) {
sqlValue = [[NSString alloc] initWithData:sqlData encoding:NSUTF8StringEncoding];
}
[bindingModel userSetValueForModel:property value:sqlValue ?: sqlData];
}
}
Expand Down
88 changes: 66 additions & 22 deletions LKDBHelper/Helper/NSObject+LKModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -229,56 +229,100 @@ - (void)modelSetValue:(LKDBProperty *)property value:(NSString *)value
///当找不到 class 时,就是 基础类型 int,float CGRect 之类的
NSString *columnType = property.propertyType;
if ([LKSQL_Convert_FloatType rangeOfString:columnType].location != NSNotFound) {
modelValue = [[LKDBUtils numberFormatter] numberFromString:value];
if (value) {
modelValue = [[LKDBUtils numberFormatter] numberFromString:value];
}
}
else if ([LKSQL_Convert_IntType rangeOfString:columnType].location != NSNotFound) {
modelValue = [[LKDBUtils numberFormatter] numberFromString:value];
if (value) {
modelValue = [[LKDBUtils numberFormatter] numberFromString:value];
}
}
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
else if ([columnType isEqualToString:@"CGRect"]) {
CGRect rect = CGRectFromString(value);
modelValue = [NSValue valueWithCGRect:rect];
if (value) {
CGRect rect = CGRectFromString(value);
modelValue = [NSValue valueWithCGRect:rect];
}
else {
modelValue = [NSValue valueWithCGRect:CGRectZero];
}
}
else if ([columnType isEqualToString:@"CGPoint"]) {
CGPoint point = CGPointFromString(value);
modelValue = [NSValue valueWithCGPoint:point];
if (value) {
CGPoint point = CGPointFromString(value);
modelValue = [NSValue valueWithCGPoint:point];
}
else {
modelValue = [NSValue valueWithCGPoint:CGPointZero];
}
}
else if ([columnType isEqualToString:@"CGSize"]) {
CGSize size = CGSizeFromString(value);
modelValue = [NSValue valueWithCGSize:size];
if (value) {
CGSize size = CGSizeFromString(value);
modelValue = [NSValue valueWithCGSize:size];
}
else {
modelValue = [NSValue valueWithCGSize:CGSizeZero];
}
}
else if ([columnType isEqualToString:@"_NSRange"]) {
NSRange range = NSRangeFromString(value);
modelValue = [NSValue valueWithRange:range];
if (value) {
NSRange range = NSRangeFromString(value);
modelValue = [NSValue valueWithRange:range];
}
else {
modelValue = [NSValue valueWithRange:NSMakeRange(0, 0)];
}
}
#else
else if ([columnType hasSuffix:@"Rect"]) {
NSRect rect = NSRectFromString(value);
modelValue = [NSValue valueWithRect:rect];
if (value) {
NSRect rect = NSRectFromString(value);
modelValue = [NSValue valueWithRect:rect];
}
else {
modelValue = [NSValue valueWithRect:NSZeroRect];
}
}
else if ([columnType hasSuffix:@"Point"]) {
NSPoint point = NSPointFromString(value);
modelValue = [NSValue valueWithPoint:point];
if (value) {
NSPoint point = NSPointFromString(value);
modelValue = [NSValue valueWithPoint:point];
}
else {
modelValue = [NSValue valueWithPoint:NSZeroPoint];
}
}
else if ([columnType hasSuffix:@"Size"]) {
NSSize size = NSSizeFromString(value);
modelValue = [NSValue valueWithSize:size];
if (value) {
NSSize size = NSSizeFromString(value);
modelValue = [NSValue valueWithSize:size];
}
else {
modelValue = [NSValue valueWithSize:NSZeroSize];
}
}
else if ([columnType hasSuffix:@"Range"]) {
NSRange range = NSRangeFromString(value);
modelValue = [NSValue valueWithRange:range];
if (value) {
NSRange range = NSRangeFromString(value);
modelValue = [NSValue valueWithRange:range];
}
else {
modelValue = [NSValue valueWithRange:NSMakeRange(0, 0)];
}
}
#endif
///如果都没有值 默认给个0
if (modelValue == nil) {
modelValue = [NSNumber numberWithInt:0];
modelValue = @0;
}
}
else if (!value || ![value isKindOfClass:[NSString class]] || ![value length]) {
//为了不继续遍历
else if (!value || ![value isKindOfClass:[NSString class]]) {
//不继续遍历
}
else if ([columnClass isSubclassOfClass:[NSString class]]) {
modelValue = value;
modelValue = [value copy];
}
else if ([columnClass isSubclassOfClass:[NSNumber class]]) {
modelValue = [[LKDBUtils numberFormatter] numberFromString:value];
Expand Down

0 comments on commit 4dec54e

Please sign in to comment.