对象未被添加到NSMutableArray中 Objective-C

3

我想简单地向可变数组添加对象,但是它们不会插入。我没有得到错误或任何提示,也无法弄清楚发生了什么。

在我的主代理文件中,我将一个数组拆分成了4个单独的字符串,像这样:

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];

我已经追踪了字符串值并且它们确实存在。

接下来,我尝试将这些字符串值添加到可变数组中。

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

当我在调试器中检查数组时,它们是空的。我做错了什么?

promoTabOptionEvents类像这样:

导入

@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;


}
2个回答

7

您能添加初始化NSMutableArray实例的代码吗?我认为您可能忘记了初始化数组,因此您的addObject调用被吞噬而没有效果。


我没有初始化可变数组。@synthesize 不会处理这个吗?我应该感到尴尬吗? - dubbeat
3
只是一点点 :-) 属性定义只是定义了指针,你需要进行初始化类型调用,然后执行 [[NSMutableArray alloc] init]。 - AlBlue

1
你有在任何地方实例化属性吗?如果是这样,你是否已经通过调试来验证了这一点?否则,你可能会向 nil 发送消息,这将没有任何效果。或者,你可能会在此调用之后创建数组,这会使它们看起来好像没有被添加。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接