从代码中的实体获取值列表

发布于 2024-10-19 08:23:49 字数 1098 浏览 1 评论 0原文

我有一个名为 clientsentity 和一个名为 clientsControllerNSArrayController。在我的 clients 实体中,我有一个名为 clientCompany 的属性。

在运行循环时以编程方式获取每个 clientCompany 的列表(以便代码可以从每个发现的 clientCompany 继续执行)的最直接方法是什么?我不确定在这种情况下我是否应该访问数组控制器或托管对象。

我试过了:

for (NSDictionary *key in clientsController) {
    NSLog(@"%@", [key objectForKey:@"clientCompany"]);
}

没有运气,但我想我还差得很远。出于好奇,我还尝试打印数组的排列对象,但它打印为空:

NSLog(@"%@", [clientsController arrangedObjects]);

clientsController 已在程序中的其他位置初始化并访问,所以我认为这很简单,但我非常这一切都是新的。谢谢。


Update
I've had a small bit of success going the NSManagedObjectContext route and trying NSFetchRequest. Requesting ObjectAtIndex:0 I can now print the first clientCompany string to the console. I just need to be able to pull all them out in a loop but the hard part is done (I hope).

I've got an entity called clients and an NSArrayController called clientsController. In my clients entity I have an attribute called clientCompany.

What is the most straightforward way of programmatically getting a list of each clientCompany while running a loop (so that code can follow on from each discovered clientCompany)? I'm not sure whether I should be accessing the array controller or the managed object in this case.

I've tried:

for (NSDictionary *key in clientsController) {
    NSLog(@"%@", [key objectForKey:@"clientCompany"]);
}

with no luck, but I think I'm way off on that. I've also tried printing the arrangedobjects of the array, out of curiosity, but it prints empty:

NSLog(@"%@", [clientsController arrangedObjects]);

clientsController has been initialised and accessed elsewhere in the program so I thought it would be straightforward but I'm very new to all this. Thanks.


Update


I've had a small bit of success going the NSManagedObjectContext route and trying NSFetchRequest. Requesting ObjectAtIndex:0 I can now print the first clientCompany string to the console. I just need to be able to pull all them out in a loop but the hard part is done (I hope).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女的英雄梦 2024-10-26 08:23:49

这对我有用:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *clientsMoc= [clientsController managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Clients" inManagedObjectContext:clientsMoc];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *items = [clientsMoc executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
NSInteger *counter;
counter = 0;
for (NSString *s in items) {
    NSManagedObject *mo = [items objectAtIndex:counter];  // assuming that array is not empty
    id value = [mo valueForKey:@"clientCompany"];
    NSLog(@"a value is %@", value);
    counter = counter + 1;
}

This worked for me:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *clientsMoc= [clientsController managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Clients" inManagedObjectContext:clientsMoc];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *items = [clientsMoc executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
NSInteger *counter;
counter = 0;
for (NSString *s in items) {
    NSManagedObject *mo = [items objectAtIndex:counter];  // assuming that array is not empty
    id value = [mo valueForKey:@"clientCompany"];
    NSLog(@"a value is %@", value);
    counter = counter + 1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文