Skip to content

Commit 0dbdc67

Browse files
author
ljh
committed
增加NSURL支持 & code clean
1 parent a01b597 commit 0dbdc67

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

Demo-iOS/iOS-Demo/AppDelegate.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ -(void)test
7272
LKTest* test = [[LKTest alloc]init];
7373
test.name = @"zhan san";
7474
test.age = 16;
75+
test.url = [NSURL URLWithString:@"http://url"];
7576

7677
//外键 foreign key
7778
LKTestForeign* foreign = [[LKTestForeign alloc]init];

Demo-iOS/iOS-Demo/LKTestModels.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
@property(strong,nonatomic) LKTestForeign* nestModel;
3030

31+
@property(copy, nonatomic) NSURL* url;
3132
@property(copy,nonatomic)NSString* name;
3233
@property NSUInteger age;
3334
@property BOOL isGirl;

LKDBHelper/Helper/NSObject+LKModel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494

9595
//lkdbhelper use
9696
- (id)modelGetValue:(LKDBProperty*)property;
97-
- (void)modelSetValue:(LKDBProperty*)property value:(id)value;
97+
- (void)modelSetValue:(LKDBProperty*)property value:(NSString*)value;
9898

9999
- (id)singlePrimaryKeyValue;
100100
- (BOOL)singlePrimaryKeyValueIsEmpty;

LKDBHelper/Helper/NSObject+LKModel.m

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ - (NSInteger)rowid
6666
return [objc_getAssociatedObject(self, &LKModelBase_Key_RowID) integerValue];
6767
}
6868

69-
- (void)setDb_tableName:(NSString*)db_tableName
69+
- (void)setDb_tableName:(NSString *)db_tableName
7070
{
7171
objc_setAssociatedObject(self, &LKModelBase_Key_TableName, db_tableName, OBJC_ASSOCIATION_COPY_NONATOMIC);
7272
}
@@ -91,12 +91,12 @@ - (void)setDb_inserting:(BOOL)db_inserting
9191
objc_setAssociatedObject(self, &LKModelBase_Key_Inserting, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
9292
}
9393
#pragma 无关紧要的
94-
+ (NSString*)getDBImagePathWithName:(NSString*)filename
94+
+ (NSString*)getDBImagePathWithName:(NSString *)filename
9595
{
9696
NSString* dir = [NSString stringWithFormat:@"dbimg/%@", NSStringFromClass(self)];
9797
return [LKDBUtils getPathForDocuments:filename inDir:dir];
9898
}
99-
+ (NSString*)getDBDataPathWithName:(NSString*)filename
99+
+ (NSString*)getDBDataPathWithName:(NSString *)filename
100100
{
101101
NSString* dir = [NSString stringWithFormat:@"dbdata/%@", NSStringFromClass(self)];
102102
return [LKDBUtils getPathForDocuments:filename inDir:dir];
@@ -112,7 +112,7 @@ + (NSDateFormatter*)getModelDateFormatter
112112
}
113113

114114
///get
115-
- (id)modelGetValue:(LKDBProperty*)property
115+
- (id)modelGetValue:(LKDBProperty *)property
116116
{
117117
id value = [self valueForKey:property.propertyName];
118118
id returnValue = value;
@@ -180,8 +180,8 @@ - (id)modelGetValue:(LKDBProperty*)property
180180
NSData* datas = UIImageJPEGRepresentation(value, 1);
181181
#else
182182
[value lockFocus];
183-
NSBitmapImageRep* srcImageRep = [NSBitmapImageRep imageRepWithData:[value TIFFRepresentation]];
184-
NSData* datas = [srcImageRep representationUsingType:NSJPEGFileType properties:@{}];
183+
NSBitmapImageRep *srcImageRep = [NSBitmapImageRep imageRepWithData:[value TIFFRepresentation]];
184+
NSData *datas = [srcImageRep representationUsingType:NSJPEGFileType properties:@{}];
185185
[value unlockFocus];
186186
#endif
187187
[datas writeToFile:[self.class getDBImagePathWithName:filename]
@@ -192,12 +192,15 @@ - (id)modelGetValue:(LKDBProperty*)property
192192
else if ([value isKindOfClass:[NSData class]]) {
193193
long random = arc4random();
194194
long date = [[NSDate date] timeIntervalSince1970];
195-
NSString* filename = [NSString stringWithFormat:@"data%ld%ld", date & 0xFFFFF, random & 0xFFF];
195+
NSString *filename = [NSString stringWithFormat:@"data%ld%ld", date & 0xFFFFF, random & 0xFFF];
196196

197197
[value writeToFile:[self.class getDBDataPathWithName:filename] atomically:YES];
198198

199199
returnValue = filename;
200200
}
201+
else if ([value isKindOfClass:[NSURL class]]) {
202+
returnValue = [value absoluteString];
203+
}
201204
else {
202205
if ([value isKindOfClass:[NSArray class]]) {
203206
returnValue = [self db_jsonObjectFromArray:value];
@@ -215,7 +218,7 @@ - (id)modelGetValue:(LKDBProperty*)property
215218
}
216219

217220
///set
218-
- (void)modelSetValue:(LKDBProperty*)property value:(id)value
221+
- (void)modelSetValue:(LKDBProperty *)property value:(NSString *)value
219222
{
220223
///参试获取属性的Class
221224
Class columnClass = NSClassFromString(property.propertyType);
@@ -224,8 +227,7 @@ - (void)modelSetValue:(LKDBProperty*)property value:(id)value
224227

225228
if (columnClass == nil) {
226229
///当找不到 class 时,就是 基础类型 int,float CGRect 之类的
227-
228-
NSString* columnType = property.propertyType;
230+
NSString *columnType = property.propertyType;
229231
if ([LKSQL_Convert_FloatType rangeOfString:columnType].location != NSNotFound) {
230232
modelValue = [[LKDBUtils numberFormatter] numberFromString:value];
231233
}
@@ -272,7 +274,7 @@ - (void)modelSetValue:(LKDBProperty*)property value:(id)value
272274
modelValue = [NSNumber numberWithInt:0];
273275
}
274276
}
275-
else if ([value length] == 0) {
277+
else if (!value || ![value isKindOfClass:[NSString class]] || ![value length]) {
276278
//为了不继续遍历
277279
}
278280
else if ([columnClass isSubclassOfClass:[NSString class]]) {
@@ -292,41 +294,37 @@ - (void)modelSetValue:(LKDBProperty*)property value:(id)value
292294
}
293295
}
294296
else if ([columnClass isSubclassOfClass:[LKDBColor class]]) {
295-
NSString* color = value;
296-
NSArray* array = [color componentsSeparatedByString:@","];
297+
NSString *colorString = value;
298+
NSArray *array = [colorString componentsSeparatedByString:@","];
297299
float r, g, b, a;
298300
r = [[array objectAtIndex:0] floatValue];
299301
g = [[array objectAtIndex:1] floatValue];
300302
b = [[array objectAtIndex:2] floatValue];
301303
a = [[array objectAtIndex:3] floatValue];
302-
304+
303305
modelValue = [LKDBColor colorWithRed:r green:g blue:b alpha:a];
304306
}
305307
else if ([columnClass isSubclassOfClass:[LKDBImage class]]) {
306-
NSString* filename = value;
307-
NSString* filepath = [self.class getDBImagePathWithName:filename];
308+
NSString *filename = value;
309+
NSString *filepath = [self.class getDBImagePathWithName:filename];
308310
if ([LKDBUtils isFileExists:filepath]) {
309-
LKDBImage* img = [[LKDBImage alloc] initWithContentsOfFile:filepath];
310-
modelValue = img;
311-
}
312-
else {
313-
modelValue = nil;
311+
modelValue = [[LKDBImage alloc] initWithContentsOfFile:filepath];
314312
}
315313
}
316314
else if ([columnClass isSubclassOfClass:[NSData class]]) {
317-
NSString* filename = value;
318-
NSString* filepath = [self.class getDBDataPathWithName:filename];
315+
NSString *filename = value;
316+
NSString *filepath = [self.class getDBDataPathWithName:filename];
319317
if ([LKDBUtils isFileExists:filepath]) {
320-
NSData* data = [NSData dataWithContentsOfFile:filepath];
321-
modelValue = data;
322-
}
323-
else {
324-
modelValue = nil;
318+
modelValue = [NSData dataWithContentsOfFile:filepath];
325319
}
326320
}
321+
else if ([columnClass isSubclassOfClass:[NSURL class]]) {
322+
NSString *urlString = value;
323+
modelValue = [NSURL URLWithString:urlString];
324+
}
327325
else {
328326
modelValue = [self db_modelWithJsonValue:value];
329-
if ([modelValue isKindOfClass:columnClass] == NO) {
327+
if (![modelValue isKindOfClass:columnClass]) {
330328
modelValue = nil;
331329
}
332330
}

0 commit comments

Comments
 (0)