2013-10-08 33 views
0

我有一个NSDictionary中称为itemDict和打印从NSDictionary中获取密钥值与GOOGLEPLUS类

NSLog(@"itemDictValues:%@",itemDict); 

输出是在该格式时:

itemDictValues: 
GTLPlusPerson 0xab821e0: 
{etag:""LTv_6IJISeUQGTVXLjMeOtebkoM/eup2crXcelmpMFKesXWlGkJjCiE"" kind:"plus#person" id:"1145282979128841" objectType:"person" displayName:"FirstName LastName" image:{url} url:"https://plus.google.com/1145282979128841"} 

从这我需要提取对应的值到ID,displayNameurl到我的NSString变量,在这种格式

profileId=1145282979128841; 

Name=FirstName LastName; 

Profilepic=https://plus.google.com/1145282979128841; 

我该如何解决这个问题?

+0

你说itemDict是一个NSDictionary,但它不会出现基于你说它不响应o​​bjectForKey。你可以在itemDict的设置方法上显示一些代码吗?它不是一个NSDictionary,而是一个GTLPlusPerson? –

+0

NSMutableArray * friends = [NSMutableArray array]; for(NSDictionary * itemDict in peopleFeed.items) { SocialProfile * friend = [[SocialProfile alloc] initWithDictionary:itemDict socialMedia:kSocialMediaGooglePlus]; [friends addObject:friend]; } – Gamerlegend

+0

GTLPlusPeopleFeed * peopleFeed,是如何定义peopleFeed的。 – Gamerlegend

回答

4

试试这个...

_profileId=(NSString*)((GTLPlusPerson*)itemDict).identifier; 
_profileName= (NSString*)((GTLPlusPerson*)itemDict).displayName; 
_profileImageURLPath=(NSString*)((GTLPlusPerson*)itemDict).image.url; 
+0

这就像魅力,感谢:) – Gamerlegend

0

对于一个NSDictionary

 NSString *profileId = itemDict[@"id"] 
    NSString *name = itemDict[@"displayName"] 
    NSString *profilePic = itemDict[@"image"][@"url"] 

如果是GTLPlusPerson对象,然后用Objective-C的点语法

 NSString *profileId = itemDict.id; 
    NSString *name = itemDict.displayName; 
    NSString *profilePic = itemDict.url; 

访问属性由你的日志,似乎这是一个GTLPlusPerson对象,而不是看一个NSDictionary

+0

抛出错误NSInvalidArgumentException',原因:' - [GTLPlusPerson objectForKey:]:无法识别的选择发送到实例0xae49b60' – Gamerlegend

+0

我不知道你解析的对象。你解析一个NSDictionary或GTLPlusPerson? –

+0

查看更新的答案 –

0

你只是解析词典来获得你想要的信息。

NSString *profileId = [itemDict objectForKey:@"id"]; 
NSString *displayName = [itemDict objectForKey:@"displayName"]; 

现在,我认为这是一个字典本身。而且,由于你没有指定那是什么字典里面,所以这就是我认为你应该做的:

NSDictionary *imageDict = [itemDict objectForKey:@"image"]; 
NSURL *imageURL = [imageDict objectForKey:@"url"]; 

然后得到的UIImage变得非常简单:

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:imageURL]; 
NSData *imageData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
UIImage *image = [[UIImage alloc] initWithData:imageData]; 

你应该选择sendAsynchronousRequest方法它发生在后台并且不支持你的应用程序的功能。

+0

NSString * profileId = [itemDict objectForKey:@“id”] does not work,throws NSInvalidArgumentException',reason:' - [GTLPlusPerson objectForKey:]:无法识别的选择器发送到实例0xae49b60' – Gamerlegend

0
NSArray* peopleList = peopleFeed.items; 
NSLog(@"peopleList %@ ",peopleList.description); 
for (NSArray *dict in peopleFeed.items) { 
        NSString *peopleStrID=(NSString*)((GTLPlusPerson*)dict).identifier; 
        NSLog(@"peopleStrID %@",peopleStrID); 
        NSString *peopleName = (NSString*)((GTLPlusPerson*)dict).displayName; 
        NSString *peoplePic = (NSString*)((GTLPlusPerson*)dict).image.url; 
       } 
0

最简单的方法是使用代码谷歌提供:

if ([[GPPSignIn sharedInstance] authentication]) { 
     // The user is signed in. 

     GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"]; 


     GTLServicePlus* plusService = [[GTLServicePlus alloc] init]; 
      plusService.retryEnabled = YES; 

     //auth = GTMOAuth2Authentication object from login   

     [plusService setAuthorizer:auth]; 

     [plusService executeQuery:query 
       completionHandler:^(GTLServiceTicket *ticket, 
            GTLPlusPerson *person, 
            NSError *error) { 
        if (error) { 
         GTMLoggerError(@"Error: %@", error); 
        } else { 
         // Retrieve the display name and "about me" text 
         NSString *description = [NSString stringWithFormat:@"%@\n%@", person.displayName, person.aboutMe]; 
         NSString *imageURL = person.image.url; 

        } 
       }]; 



    } else { 
     //user is logged out 
    } 
}