ios - 初始化对象时出错。无法为 PFObject 设置值

标签 ios objective-c parse-platform properties null

我不明白为什么我无法设置 PFObject 实例的值。它在应用程序的其他地方成功运行,但我一定是在这个 View 中做错了什么。非常感谢您帮助我弄清楚我遗漏了什么。谢谢。

基本上

self.ABC.type = @"Frustrated";
NSLog(@"why is this null? -----> %@",self.ABC.type);
2015-10-18 18:26:29.277 XX [885:109003] why is this null?  -----> (null)

为什么没有赋值?它应该记录为 ... I'm not null or fresting... I'm working, see -----> Frustrated !!!!!! 但它不起作用!

因此我无法在 Parse 中设置对象,因为它为 nil 并且应用程序崩溃了。

下面应该是您需要的全部和更多代码,但如果您有任何问题或建议,请告诉我。谢谢!:

FCollectionViewController.m:

#import "FCollectionViewController.h"
#import "FCell.h"
#import "SectionHeaderView.h"

#import "MBProgressHUD.h"

#import "Helper.h"

@interface FCollectionViewController()
 <UICollectionViewDelegate, MBProgressHUDDelegate>

@property (nonatomic, strong) NSDictionary *presetsDictionary;
@property (nonatomic, strong) NSArray *presets;
@property (nonatomic, strong) NSIndexPath *textViewIndexPath;
@property (nonatomic, strong) FCell *fCell;

@property (nonatomic, strong) MBProgressHUD *HUD;
@property (nonatomic, strong) NSArray *headTitleArray;

@end

@implementation FCollectionViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.headTitleArray = [NSArray arrayWithObjects:
                           @"type",
                           @"model",
                           @"feature", nil];
    PFQuery *query = [PFQuery queryWithClassName:@"Presets"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        NSMutableArray *keys = [[NSMutableArray alloc] init];
        NSMutableArray *values = [[NSMutableArray alloc] init];

        for (PFObject *obj in objects) {
            [keys addObject:[obj objectForKey:@"key"]];
            [values addObject:[obj objectForKey:@"value"]];
        }
        self.presetsDictionary = [NSDictionary dictionaryWithObjects:values forKeys:keys];

        self.presets = [[NSMutableArray alloc] initWithObjects:
                            [self.presetsDictionary objectForKey:@"type"],
                            [self.presetsDictionary objectForKey:@"model"],
                            [self.presetsDictionary objectForKey:@"feature"],nil];
        NSLog(@"self.presets ----> %@", self.presets);
        [self.collectionView reloadData];
    }];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return [self.presets count];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return  [self.presets[section] count];
}

- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView * view = nil;

    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        ItemSectionHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader                                                      withReuseIdentifier:NSStringFromClass([ItemSectionHeaderView class])
                                                                                        forIndexPath:indexPath];
        header.captionLabel.text = self.headTitleArray[indexPath.section];
        view = header;
    }
    return view;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell;

    FCell *aCell = (FCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"fCell" forIndexPath:indexPath];
    aCell.label.text = self.presets[indexPath.section][indexPath.row];
    aCell.label.textAlignment = NSTextAlignmentCenter;
    cell = aCell;
    return cell;
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize size;
     ....
    return size;
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
  NSArray * selectedRows = self.collectionView.indexPathsForSelectedItems;

  for (NSIndexPath * selectedRow in selectedRows) {
    if ((selectedRow.section == indexPath.section) && (selectedRow.row != indexPath.row)) {
      [self.collectionView deselectItemAtIndexPath:selectedRow animated:NO];
    }
  }
    switch (indexPath.section) {
    case 0:
      self.aBC.type = @"Frustrated";
      NSLog(@"why isn't this being assigned? -----> %@",self.ABC.type);
      break;
    case 1:
      self.aBC.model = self.presets[indexPath.section][indexPath.row];
      NSLog(@"why is this null?!!!! -----> %@",self.ABC.model);
      NSLog(@"this DOES log the value I want!! -----> %@",self.presets[indexPath.section][indexPath.row]);
      break;
    case 2:
      ...
    default:
      break;
    }
}

FCollectionViewController.h:

#import <UIKit/UIKit.h>
#import "ABC.h"

@interface FCollectionViewController : UICollectionViewController
//<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) ABC *aBC;
@end

ABC.h:

#import <Foundation/Foundation.h>

@interface ABC : NSObject

@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSString *model;
@property (nonatomic, strong) NSString *feature;
@property (nonatomic, strong) PFObject *pfObj;
@property (nonatomic, strong) PFUser *user;

- (id)initWithPFObject:(PFObject *)anObject;

@end

ABC.m:

#import "ABC.h"

@implementation ABC

- (id)initWithPFObject:(PFObject *)anObject
{
    if(self = [super init])
    {
        [anObject fetchIfNeeded];
        self.pfObj = anObject;
        self.type = [anObject objectForKey:@"type"];
        self.model = [anObject objectForKey:@"model"];
        ...
    }
    return self;
}

@end

FCell.h:

#import <UIKit/UIKit.h>

@interface FCell : UICollectionViewCell

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

@end

FCell.m:

#import "FCell.h"

@implementation FCell

-(void)setHighlighted:(BOOL)highlighted
{
  [super setHighlighted:highlighted];
  self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
  self.selectedBackgroundView.backgroundColor = [UIColor darkGrayColor];

  [self setNeedsDisplay];
}

@end

最佳答案

aBC 没有在任何地方初始化。你声明了它,但它没有被初始化。所以设置 aBC.type 实际上并没有设置任何东西。

在您的 viewDidLoad 方法中添加 self.aBC = [[ABC alloc] init];

关于ios - 初始化对象时出错。无法为 PFObject 设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33208030/

相关文章:

ios - 从选项卡栏项目按执行模态和推送 Segue

ios - 单击后退或前进时 MPMovieplayerController 进入黑屏

ios - 如何注意 UITableViewCell 是否离开了可见区域?

iphone - QuickDialog 的多行内联条目元素

JavaScript/解析 : Follow wont return 'to' value

ios - 在 Xcode 调试中观察变量或检查对象的技巧

objective-c - 如何更改 NSURL 文件名

objective-c - 在 Swift 中将 NSData 转换为 Data,现在当我尝试将字节转换为 [UInt] 时出现崩溃

objective-c - Parse.com 获取用户名(如果是 facebook 或解析用户)

iphone - 解析 iPhone 中的推送通知