gpt4 book ai didi

ios - AFRESTClient pathForEntity具有嵌套资源?

转载 作者:行者123 更新时间:2023-12-01 16:53:00 25 4
gpt4 key购买 nike

我正在尝试使用嵌套的资源URL在相册中获取照片,例如:

GET http://www.myapi.com/albums/:album_id/photos

似乎最简单的方法是通过覆盖我的AFRESTClient子类中的pathForEntity函数。但是,我无权访问相册对象,因此无法返回包含album_id的URL。我应该如何覆盖/扩展来完成此任务?请参阅下文,了解我如何尝试执行此操作,请注意我无法提供相册ID的问号:
- (NSString *)pathForEntity:(NSEntityDescription *)entity {

NSString *path = AFPluralizedString(entity.name);

if ([entity.name isEqualToString:@"Photo"]) {
path = [NSString stringWithFormat:@"albums/%d/photos", ??];
}

return path;
}

在堆栈的更高处,我在PhotosViewController -viewDidLoad中拥有它
- (void)viewDidLoad
{
[super viewDidLoad];

self.title = self.currentAlbum.name;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Photo"];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:NO]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"album == %@", self.currentAlbum];
fetchRequest.predicate = predicate;

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController.delegate = self;
[self.fetchedResultsController performFetch:nil];
}

谢谢!

最佳答案

我们具有相似的层次结构,最终在我们的AFRESTClient子类中实现pathForRelationship:forObject:方法。以您的相册和照片为例,我想它会是这样的:

- (NSString *)pathForRelationship:(NSRelationshipDescription *)relationship
forObject:(NSManagedObject *)object
{
if ([object isKindOfClass:[Album class]] && [relationship.name isEqualToString:@"photos"]) {
Album *album = (Album*)object;
return [NSString stringWithFormat:@"/albums/%@/photos", album.album_id];
}
return [super pathForRelationship:relationship forObject:object];
}

这是假设您在模型中设置了toMany关系。

关于ios - AFRESTClient pathForEntity具有嵌套资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14127123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com