Objects not being added to NSMutableArray Objective -C

跟風遠走 提交于 2020-01-11 03:28:12

问题


I'm trying to simply add objects to a mutable array but they WILL NOT insert. I'm not getting errors or anything and I can't figure out whats going on.

In my main delegate file I split an array into 4 separate strings like so.

NSArray *split=[currentParsedCharacterData componentsSeparatedByString:@"|"];
        NSLog([split objectAtIndex:3]);

        NSString *date=[split objectAtIndex:0];
        NSString *venue=[split objectAtIndex:1];
        NSString *event=[split objectAtIndex:2];
        NSString *city=[split objectAtIndex:3];

I've traced out the string values and they are definitely there.

Up next I try to add these string values to mutable arrays

[self.promoTabOptionEvents.dates addObject:date];
        [self.promoTabOptionEvents.venues addObject:venue];
        [self.promoTabOptionEvents.event addObject:event];
        [self.promoTabOptionEvents.city addObject:city];

When I check the arrays in the debugger they are empty. What am I doing wrong?

promoTabOptionEvents class looks like this

import

@interface PromoTabOptionEvents : UIViewController {

    NSString *event_headline;
    NSMutableArray *dates;
    NSMutableArray *venues;
    NSMutableArray *event;
    NSMutableArray *city;

}

@property(nonatomic,retain)NSString *event_headline;
@property(nonatomic,retain)NSMutableArray *dates;
@property(nonatomic,retain)NSMutableArray *venues;
@property(nonatomic,retain)NSMutableArray *event;
@property(nonatomic,retain)NSMutableArray *city;
-(void)applyLabels;
-(id)initWithTabBar;
@end


#import "PromoTabOptionEvents.h"


@implementation PromoTabOptionEvents
@synthesize event_headline;
@synthesize dates;
@synthesize venues;
@synthesize event;
@synthesize city;

-(id) initWithTabBar {
    if ([self init]) {
        //this is the label on the tab button itself
        //self.title = @"Tab1";

        //use whatever image you want and add it to your project
        self.tabBarItem.image = [UIImage imageNamed:@"events.png"];

        // set the long name shown in the navigation bar
        self.navigationItem.title=@"Events";


        CGRect bgframe;
        bgframe.size.width=320; bgframe.size.height=460;
        bgframe.origin.x=0; bgframe.origin.y=0;
        UIImage* bgimage = [UIImage imageNamed:@"eventsbig.png"];
        UIImageView *imagebgview = [[UIImageView alloc] initWithImage: bgimage];
        imagebgview.frame=bgframe;
        imagebgview.contentMode=UIViewContentModeScaleAspectFit;
        self.view.backgroundColor=[UIColor whiteColor];
        [self.view addSubview:imagebgview];


    }
    return self;


}

回答1:


Can you add the code where you initialize your NSMutableArray instances? I think you might have forgotten to initialise the arrays and your addObject calls are being swallowed up with no effect.




回答2:


Are you instantiating the properties anywhere, and if so, have you debugged through to verify that is the case? Otherwise you may be sending messages to nil, which will have no effect. Alternatively, you may be doing the array creation after this call, which would make it look like they're not added.



来源:https://stackoverflow.com/questions/2341439/objects-not-being-added-to-nsmutablearray-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!