0

我有一个名为 itemDict 的 NSDictionary 并且在打印时

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"}

从中我需要提取与iddisplayNameurl对应的值到我的 NSString 变量中,格式如下

profileId=1145282979128841;

Name=FirstName LastName;

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

我该怎么做?

4

5 回答 5

4

试试这个 ...

_profileId=(NSString*)((GTLPlusPerson*)itemDict).identifier;
_profileName= (NSString*)((GTLPlusPerson*)itemDict).displayName;
_profileImageURLPath=(NSString*)((GTLPlusPerson*)itemDict).image.url;
于 2013-10-09T05:57:20.513 回答
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

于 2013-10-08T16:55:17.287 回答
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 方法,因为它发生在后台并且不会阻碍您的应用程序的功能。

于 2013-10-08T17:56:39.847 回答
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;
                }
于 2015-04-27T12:30:10.680 回答
0

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

    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
    }
}
于 2015-07-07T14:06:51.120 回答