我正在尝试获取专辑第一张图片的网址。我从json webservice获取数据。这个网络服务看起来像这样。
"albums": [
{
"name": "Club Brugge - KRC Genk",
"date": "08.10.2012",
"pic_album_id" : 1
"pictures": [
{
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg",
"picture_id" : 1
},
{
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/e8e10c0664eb0533a0534ed69891b165.jpg"
"picture_id" : 2
},
{
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/750b55a87b8eae33b8f3278add9bec44.jpg"
"picture_id" : 3
},
{
"url": "http://www.krcgenk.be/images/gallery/album_199/800X600/5867e1dedd00d08d830fd7f098a0b747.jpg"
"picture_id" : 4
}
我正在尝试只获取第一个网址。我试图通过以下功能获得此功能。
- (NSString *)getFirstPictureOfAlbumId: (int)AlbumId
{
NSString *picture_Url = [[NSString alloc]init];
NSLog(@"here1");
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSLog(@"here2");
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(picture_id == 1) AND (pic_album_id == %@)",AlbumId];
NSLog(@"here3");
[request setEntity:[NSEntityDescription entityForName:@"Picture" inManagedObjectContext:self.genkDatabase.managedObjectContext]];
NSLog(@"here4");
[request setPredicate:predicate];
NSLog(@"here5");
NSError *error = nil;
NSArray *results = [self.genkDatabase.managedObjectContext executeFetchRequest:request error:&error];
if (error != nil)
{
//handle errors
}
NSLog(@"picture url is: %@",[results valueForKey:@"url"]);
picture_Url = [results valueForKey@"url"];
return picture_Url;
}
我的日志只到达“here2”所以我认为我的谓词有问题。我的另一个问题是存储第一张图片的网址。
有人能帮助我吗?
提前致谢
修改
这就是我在tableview中所做的事情
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[self getFirstPictureOfAlbumId:indexPath.row+1]]];
NSLog(@"indexpatch is: %d",indexPath.row+1);
NSLog(@"first picture is: %@",[self getFirstPictureOfAlbumId:indexPath.row+1]);
然后它就像这样记录下来。
012-10-09 13:17:58.140 RacingGenk[6069:c07] album id is: 1
2012-10-09 13:17:58.311 RacingGenk[6069:c07] indexpatch is: 1
2012-10-09 13:17:58.311 RacingGenk[6069:c07] album id is: 1
2012-10-09 13:17:58.313 RacingGenk[6069:c07] first picture is: http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg
2012-10-09 13:17:58.314 RacingGenk[6069:c07] album id is: 2
2012-10-09 13:17:58.445 RacingGenk[6069:c07] indexpatch is: 2
2012-10-09 13:17:58.445 RacingGenk[6069:c07] album id is: 2
2012-10-09 13:17:58.446 RacingGenk[6069:c07] first picture is: http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg
2012-10-09 13:17:58.447 RacingGenk[6069:c07] album id is: 3
2012-10-09 13:17:58.577 RacingGenk[6069:c07] indexpatch is: 3
2012-10-09 13:17:58.577 RacingGenk[6069:c07] album id is: 3
2012-10-09 13:17:58.578 RacingGenk[6069:c07] first picture is: http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg
EDIT2
LOG
RacingGenk[6605:c07] picture_id=1, album_id=1, url=http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg
2012-10-09 13:57:40.361 RacingGenk[6605:c07] picture_id=1, album_id=2, url=http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg
2012-10-09 13:57:40.525 RacingGenk[6605:c07] picture_id=1, album_id=3, url=http://www.krcgenk.be/images/gallery/album_199/800X600/1a06dc0e405fd0219e3d327f1eec7fbf.jpg
答案 0 :(得分:1)
在你的谓词中
[NSPredicate predicateWithFormat:@"(picture_id == 1) AND (pic_album_id == %@)", AlbumId];
你使用"%@"作为格式说明符(用于对象),但是 int 参数AlbumId
。
如果" pic_album_id"属性在核心数据模型中存储为NSNumber
,然后谓词必须
[NSPredicate predicateWithFormat:@"(picture_id == 1) AND (pic_album_id == %@)",
[NSNumber numberWithInt:AlbumId]];
(注意:在Objective C中,让变量名以小写字母开头是很常见的,即albumId
而不是AlbumId
。使用该约定使在此论坛中,您的代码更易于阅读。)
编辑:要获取给定albumId
的相册中的第一张图片,以下谓词应该有效:
[NSPredicate predicateWithFormat:@"(picture_id == 1) AND (album.pic_album_id == %@)",
[NSNumber numberWithInt:albumId]];
此代码假定您与相册实体的相册实体具有相册的反向关系<与图片的反向关系)。
即使只有一个结果,它也存储在一个数组中:
NSArray *results = [self.genkDatabase.managedObjectContext executeFetchRequest:request error:&error];
if (results == nil) {
// handle errors
} else if (results.count == 0) {
// nothing found
} else {
Picture *picture = [results objectAtIndex:0];
NSLog(@"picture_id=%@, album_id=%@, url=%@", picture.picture_id, picture.album.pic_album_id, picture.url);
pictureUrl = [picture url];
}
答案 1 :(得分:0)
之前我正在使用NSPredicate,也许你可以使用这个
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == 1"];
将NSPredicate的EXACT变量名称用作从Web服务返回的内容。