Objective c 如何将NSObject的所有属性设置为nil,而不必显式指定所有属性。?
我有一个单例对象,它有一些属性,我想根据属性的类型将所有这些值设置为nil或“NO”。一种方法是编写一个重置方法,我自己将所有这些属性设置为nil,如下所示 这甚至可能有实例变量,这些变量也需要清除Objective c 如何将NSObject的所有属性设置为nil,而不必显式指定所有属性。?,objective-c,Objective C,我有一个单例对象,它有一些属性,我想根据属性的类型将所有这些值设置为nil或“NO”。一种方法是编写一个重置方法,我自己将所有这些属性设置为nil,如下所示 这甚至可能有实例变量,这些变量也需要清除 -(void)reset { //Properties self.lastLoggedInUser = nil; self.localMasterDownload = NO; self.isFirstLaunchDone = NO; self.lastArch
-(void)reset
{
//Properties
self.lastLoggedInUser = nil;
self.localMasterDownload = NO;
self.isFirstLaunchDone = NO;
self.lastArchvingDate = nil;
self.archivingDueDate = nil;
self.dbEncryptionKey = nil;
self.checkInDone = NO;
//Instance variables
_isPostionModuleExtandedMode = NO;
_isPassengerModuleExtandedMode = NO;
}
但我正在寻找一种更通用、更有效的方法
感谢您的帮助
谢谢。这将获得您的类属性,从那里您只需将键字符串转换为尝试为nil属性的内容。我想这是可能的
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
unsigned count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (int i = 0; i < count; i++) {
NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
if([self valueForKey:key]!=nil){
[dict setObject:[self valueForKey:key] forKey:key];
}else{
[dict setObject:@NO forKey:key];
}
}
free(properties);
return [NSDictionary dictionaryWithDictionary:dict];
NSMutableDictionary*dict=[NSMutableDictionary];
无符号计数;
objc_property_t*properties=class_copyPropertyList([self class],&count);
for(int i=0;i
-(无效)重置属性{
无符号整数超过,i;
objc_property_t*properties=class_copyPropertyList([self class],&outCount);
对于(i=0;i[%@]]@=%@“,[propertyName类],propertyName,值);
/*您可以添加一些其他检查*/
if([value iskindof类:[NSNumber类]]){
[self setValue:[NSNumber numberWithInteger:0]forKeyPath:propertyName];
}
if([value iskindof类:[NSString类]]){
[self-setValue:nil-forKeyPath:propertyName];
}
if([value iskindof类:[NSDictionary类]]){
[self-setValue:nil-forKeyPath:propertyName];
}
id valueRes=[self-valueForKey:propertyName];
NSLog(@“[after]XXXresetProperties=>[%@]@=%@\n--\n”、[propertyName类]、propertyName、valueRes);
}
}
自由(财产);
}
你可以在那里买到:
您发布的内容是最有效的方式。任何动态处理对象属性的方法的效率都会低得多。@rmaddy是否有任何方法可以使用objective-c运行时来使用。。?假设我有很多属性,我不想写那么多代码。。相反,我想在基类中编写一个方法,这将为我澄清这一点…?我从来没有做过你想做的事情,但从Objective-C的动态特性开始,这使得实现起来非常困难。你真正需要的是一种暴力手段。您必须对从aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa。类内省不起作用,因为可能有其他未公开的属性名。例如,NSManagedObject从
mom
文件获取其属性,而不是从类中固有的任何内容获取。或者,只需在执行过程中手动重置所有内容。是否存在无法销毁和重新分配实例的原因?
- (void) resetProperties {
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
for(i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
const char *propName = property_getName(property);
if(propName) {
NSString *propertyName = [NSString stringWithCString:propName encoding:[NSString defaultCStringEncoding]];
id value = [self valueForKey:propertyName];
NSLog(@"[berore]XXXresetProperties => [%@] %@=%@", [propertyName class],propertyName, value);
/* You can add some other checks */
if ([value isKindOfClass:[NSNumber class]]) {
[self setValue:[NSNumber numberWithInteger:0] forKeyPath:propertyName];
}
if ([value isKindOfClass:[NSString class]]) {
[self setValue:nil forKeyPath:propertyName];
}
if ([value isKindOfClass:[NSDictionary class]]) {
[self setValue:nil forKeyPath:propertyName];
}
id valueRes = [self valueForKey:propertyName];
NSLog(@"[after]XXXresetProperties => [%@] %@=%@\n---\n", [propertyName class],propertyName, valueRes);
}
}
free(properties);
}