之前的考虑是拼装一个完整的函数程序,这样整个函数会比较的大,层次多
如果采用调用函数的方式,对每个类结构生成一个对应的函数,结构会很清晰,实现也会容易些
数组这个结构导致整个实现出现坎坷,以为,如果没有特殊的说明,程序并不知道数组中存放的是什么类型的数据
如果没有数组,类结构是个传统意义上的树,是可以采用这个方法进行代码生成的 6-25
//2012-6-24 the code maker
//write to file
- (void) _writeUsernameToFile {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:@"data"];
NSString *uname = self._username;
// NSData和NSString之间的转换
NSData *data = [uname dataUsingEncoding:NSASCIIStringEncoding];
if ([data writeToFile:myFile atomically:YES]) {
return;
} else {
// 如何抛出异常
[NSException raise:@"Write Error" format:@"Cannot write to %@", myFile];
}
}
+ (NSMutableDictionary *) getDicFromObject: (id) theObject
{
if ([theObjectisKindOfClass:[NSStringclass]])
{
return theObject;
}
NSMutableDictionary * tmpDic = [[[NSMutableDictionaryalloc] init] autorelease];
if (theObject ==nil)
{
return tmpDic;
}
struct Field {
char * fieldName;
char * className;
};
//get fiels and class
getFieldArray
{
NSString *className =NSStringFromClass([theObjectclass]);
const char *cClassName = [className UTF8String];
id theClass = objc_getClass(cClassName);
unsignedint outCount, i;
objc_property_t *properties = class_copyPropertyList(theClass, &outCount);
NSMutableArray *propertyNames = [[NSMutableArrayalloc]initWithCapacity:1];
for (i = 0; i < outCount; i++)
{
objc_property_t property = properties[i];
NSString *propertyNameString = [[NSStringalloc]initWithCString:property_getName(property)encoding:NSUTF8StringEncoding];
[propertyNamesaddObject:propertyNameString];
[propertyNameStringrelease];
NSLog(@"%s %s\n",property_getName(property),property_getAttributes(property));
struct Field ff;
ff.className = ;
ff.fieldName = ;
[array add ];
}
}
//get field class
getClass: (char *)str //T@"NSString",&,N,Vm_strIcon
{
char tmpName[128] = {0};
int ilen = strlen(str);
for (int i = 0; i < ilen; i++)
{
if (str[i + 3] != '"')
{
tmpName[i] = str[i + 3];
}
else
{
break;
}
}
return //NSString ...
}
//get all the property fields names
NSString *className =NSStringFromClass([theObject class]);
const char *cClassName = [className UTF8String];
id theClass = objc_getClass(cClassName);
unsignedint outCount, i;
objc_property_t *properties = class_copyPropertyList(theClass, &outCount);
NSMutableArray *propertyNames = [[NSMutableArrayalloc] initWithCapacity:1];
for (i = 0; i < outCount; i++)
{
objc_property_t property = properties[i];
NSString *propertyNameString = [[NSStringalloc] initWithCString:property_getName(property)
encoding:NSUTF8StringEncoding];
[propertyNamesaddObject:propertyNameString];
[propertyNameStringrelease];
NSLog(@"%s %s\n",property_getName(property), property_getAttributes(property));
}
for (NSString *keyin propertyNames)
{
SEL selector = NSSelectorFromString(key);
id value = [theObject performSelector:selector];
if (value == nil)
{
value = [NSNullnull];
[tmpDic setObject:value forKey:key];
continue;
}
//[finalDict setObject:value forKey:key];
if ([value isKindOfClass:[NSStringclass]])
{
[tmpDic setObject:value forKey:key];
}
elseif ([value isKindOfClass:[NSArrayclass]])
{
NSMutableArray * valueArray = [[[NSMutableArrayalloc] init]autorelease];
NSArray * aArray = (NSArray *)value;
for (int i =0; i < [aArray count]; i++)
{
[valueArray addObject:[NetRequestManagergetDicFromObject:[aArray objectAtIndex:i]]];
//[tmpDic setObject:[NetRequestManager getDicFromObject:[aArray objectAtIndex:i]]
// forKey:key];
}
[tmpDic setObject:valueArray forKey:key];
}
else
{
[tmpDicsetObject:[NetRequestManagergetDicFromObject:value] forKey:key];
}
}
return tmpDic;
}