2015-10-19 97 views
0

我無法弄清楚爲什麼我無法設置PFObject的實例的值。它在應用程序的其他地方成功運行,但在這個視圖中,我必須做出錯誤的事情。我真的很感謝你的幫助,弄清楚我錯過了什麼。謝謝。初始化對象時出錯。無法將值設置爲PFObject

基本上

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 frustrating... I'm working, see -----> Frustrated !!!!!!但它不工作!

因此,我無法在Parse中設置對象bc它是零,應用程序崩潰。

下面應該是你需要的所有和更多的代碼,但讓我知道你有問題或建議。謝謝!:

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 
+3

您在初始化ABC對象的位置? – Pawan

+0

他不是。剛剛看到您的評論後,我的答案,但我會刪除我的,如果你張貼你的。 –

+0

謝謝@pawan。你是對的,但我不確定在哪裏/如何。嘗試這樣做時出現錯誤。 – user2985200

回答

1

ABC不被任何初始化。你聲明它,但它沒有被初始化。所以設置BC.type實際上並沒有設置任何東西。

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

+0

謝謝傑克。你是對的,我猜我不是,但我無法弄清楚它的語法。添加aBC =新ABC();正如你建議我得到的警告: – user2985200

+0

先發制人的後.... ....全文評論謝謝傑克。你是對的,我猜我不是,但我無法弄清楚它的語法。添加aBC =新ABC();正如你所建議的那樣,我會得到警告:「使用未聲明的標識符'aBC'是否意味着'_aBC'?」如果我改爲寫ABC * aBC = [[ABC alloc] init],我得到警告「未使用」(未使用的標識符'new'),看起來像是C++ vs objective c問題。變量aBC「 – user2985200

+0

Ahh,對不起,應該是_aBC = [[ABC alloc] init];或者_aBC = [ABC new];錯誤的語法,用C++幫助一個朋友,我會編輯我的答案。嘗試不工作,因爲您正在定義aBC的新本地實例,並且您的方法正在使用該屬性。_aBC = [ABC新];可能無法正常工作...我從來沒有嘗試過使用自定義類,所以我不確定它是否會自動調用[[ABC alloc] init]; ,不過我認爲這樣做可以做到這一點,因爲您可以使用PFObject和ABC繼承PFObject。 –