Reputation: 82
I can't figure out why I am unable to set the value of an instance of PFObject
. It is working successfully elsewhere in the app, but I must be doing something wrong in this view. I'd really appreciate your help figuring out what I'm missing. Thanks.
Basically
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)
Why is the value not being assigned? It should log as ... I'm not null or frustrating... I'm working, see -----> Frustrated !!!!!!
but it is not working!
I'm therefore unable to set the object in Parse bc it's nil and the app crashes.
Below should be all and more of the code you need, but let me know it you have questions or suggestions. Thanks!:
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
Upvotes: 0
Views: 87
Reputation: 4378
aBC isn't initialized anywhere. You declare it, but it isn't initialized. So setting aBC.type isn't actually setting anything.
Add self.aBC = [[ABC alloc] init];
in your viewDidLoad method.
Upvotes: 1