从Parse中为Xcode检索Int值并将其显示在标签中

时间:2014-05-18 04:03:41

标签: xcode parsing integer label

这里有一个初学者,我整天都被困在某事上了!

我有2个Parse Tables:一个用户(完全适合登录等)和一个保存变量的UserStats(在这种情况下是用户级别作为第一列)

我想要做的就是检索用户的当前级别并将其显示在Xcode中的标签中。我已经做了很多研究,但我无法解决我出错的地方。

我在我的.m中使用此查询:

    - (void) retrieveFromParse { 
       PFQuery query = [PFQuery queryWithClassName:@"UserStats"];
       [query whereKey:@"User" equalTo:[PFUser currentUser]];
       [query getFirstObjectInBackgroundWithBlock:(PFObjectobject, NSError *error) 
         {
            if (!object) 
               {
               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oh No!" message:@"Could not locate value!" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
               [alert show];
               }
            else 
               {
               int Level = [[object objectForKey:@"Level"] intValue];
               [_newlabel setText:[NSString stringWithFormat:@"%d", Level]];
               }
         }]; 
       }

我的.h看起来像这样:

     import <UIKit/UIKit.h>
     import <Parse/Parse.h>

     @interface ShowLevel : UIViewController 

     @property (weak, nonatomic) IBOutlet UILabel *newlabel;

     @end

编辑:我打开了异常断点并突出显示:

    [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {

2 个答案:

答案 0 :(得分:0)

您获得的实际错误是什么?此外,你必须做任何与主线程上的UI有关的事情。该查询实际上是在后台线程上运行,您要么显示alertView,要么更新您必须在主线程上执行的标签。将dispatch_async(dispatch_get_main_queue(), ^{ });添加到您正在执行UI的代码中

if (!object) 
               {
                  dispatch_async(dispatch_get_main_queue(), ^{
                      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oh No!" message:@"Could not locate value!" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
                      [alert show];
                  });
               }
            else 
               {
                          int Level = [[object objectForKey:@"Level"] intValue];
                    dispatch_async(dispatch_get_main_queue(), ^{
                          [_newlabel setText:[NSString stringWithFormat:@"%d", Level]];
                    });
               }

答案 1 :(得分:0)

这已经解决了。我使用.m代码:

PFQuery *query = [PFQuery queryWithClassName:@"UserStats"];
PFObject *Object = [query getObjectWithId:@"fghsd7fddhf"];
level = [[Object objectForKey:@"Level"] intValue];
[levelLabel setText:[NSString stringWithFormat:@"%i", level]];

虽然它只是暂时的,因为我需要在没有指定objectId的情况下将其更改为指向值,这对于测试非常有效。感谢所有提供帮助的人:)