【问题标题】:get the name of the property not the value获取属性的名称而不是值
【发布时间】:2013-10-31 14:04:52
【问题描述】:

我正在使用核心数据,并且一直在使用这样的代码:

[self.form setValue:self.comments.text forKey:@"comments"];

我想将这样的代码放入一个循环中,我所有的核心数据名称都与属性名称相同。我怎样才能说forKey:self.comments.name 并获得与上述相同的结果或类似的结果?

编辑:

如果这不可能,是否有另一种方法可以将大量值从属性设置到 coredata 中?我有 50 多个类似的属性和属性需要设置,并且希望避免使用我现在正在做的事情。

【问题讨论】:

  • 我可能是错的,但我认为你不能在运行时得到那个
  • 什么是self,什么是self.form
  • @property (strong, nonatomic) IBOutlet UITextView *comments; @property (strong, retain) NSManagedObject *form;

标签: ios core-data


【解决方案1】:

如果你真的想要,你可以使用 objc/runtime.h 中的这些函数:

objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount) // To get properties declared by a class.
const char *property_getName(objc_property_t property) // To get the name of one property

类似这样的:

unsigned int propCount = 0;
objc_property_t *properties = class_copyPropertyList([self class], &propCount);

for(int idx = 0; idx < propCount; idx++) {
    objc_property_t prop = *(properties + idx);
    NSString *key = @(property_getName(prop));
    NSLog(@"%@", key);
}

【讨论】:

  • 我会试一试,这似乎是我要找的。​​span>
  • 有了这个代码,我可以不使用idx得到一个属性名吗?
  • 因为你得到一个类中声明的属性的列表,你必须索引才能访问它们。
  • 这非常接近我的需要,但我宁愿不使用索引,因为我有其他属性,这样可能会搞砸。我希望从 self.cmets 获取属性名称,因此当我给它 self.cmets 时它会记录/打印“cmets”
【解决方案2】:

reading the docs on CoreData 确实没有替代品,因为如果不做一些工作,使用模式和语法根本不会很明显。

也就是说,您通常会从数据存储中获取 NSManagedObject 子类的实例:

NSManagedObjectContext* moc = [delegate managedObjectContext];
NSEntityDescription* description = [NSEntityDescription entityForName:@"Filter" inManagedObjectContext:moc];
NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity:description];
[request setSortDescriptors:[NSArray arrayWithObject:descriptor]];
NSError *error;
_enabledFilters = [NSMutableArray arrayWithArray:[moc executeFetchRequest:request error:&error]];
if (error) {
    NSLog(@"%@",error.localizedDescription);
}

在这个例子中,我现在有一个名为“Filter”的 NSManagedObject 实例数组

然后您可以选择适当的实例进行引用,并使用简单的点语法访问它的所有属性。

Filter* thisFilter = (Filter*)[_displayFilters objectAtIndex:indexPath.row];
cell.label.text = thisFilter.name;
cell.label.backgroundColor = [UIColor clearColor];
NSString*targetName = thisFilter.imageName;
UIImage *image = [UIImage imageNamed:targetName];
cell.image.image = image;

现在我已从我的持久数据存储中获取信息,并在我的应用程序中使用它。

换一种方式写入数据存储中的实例只是略有不同,因为您直接设置 NSManagedObject 子类的实例的属性,然后在上下文中调用 save 以推送任何更改去商店。

TL;DR - 您应该自己花一两个小时阅读 CoreData 文档...

【讨论】:

    【解决方案3】:

    一种方法是自己声明一个属性数组。

    NSArray *attributes = [NSArray arrayWithObjects:..., @"comments", .., nil]; // or a NSSet
    for(NSString *attribute in attributes){
        NSString *text = [[self performSelector:NSSelectorFromString(attribute)] text]; // presuming that it's safe to call 'text' on all your properties
        [self.form setValue:text forKey:attribute];
    }
    

    如果您想要核心数据模型的所有属性,也可以使用this

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多