创建NSMutableArray并添加对象后,count显示为0

时间:2014-05-21 02:13:03

标签: objective-c nsmutablearray

快速枚举有效,并显示我的3个对象的内存地址。但是我的兄弟阿瑞(兄弟Array.count)的一个简单的计数提出了0.我有点像菜鸟。我缺少什么?

我没有收到任何错误或警告。一切似乎都很好。我无法弄清楚为什么我的array.count出现为零。提前谢谢!

这是我的整个MasterViewController.m

//  MasterViewController.m
//  Matt_Bug
//
//  Created by Matthew Toto on 5/17/14.
//  Copyright (c) 2014 Matthew Toto. All rights reserved.
//

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "MPTBrothers.h"

@interface MasterViewController () {
    NSMutableArray *_objects;
}
@end

@implementation MasterViewController

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Brothers";

    MPTBrothers *First = [[MPTBrothers alloc]initWithInfo:@"JP" age:37 height:5];
    MPTBrothers *Second = [[MPTBrothers alloc]initWithInfo:@"Matt" age:33 height:6];
    MPTBrothers *Third = [[MPTBrothers alloc]initWithInfo:@"Kevin" age:27 height:5];
    NSMutableArray *brothersArray = [NSMutableArray arrayWithObjects:First, Second, Third, nil];

    for (id brotherObjects in brothersArray) {
        NSLog(@"%@", brotherObjects);
    }
    NSLog(@"The array count is %lu",brothersArray.count);

    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    MPTBrothers *brotherCell = [self.brothersArray objectAtIndex:indexPath.row];
    cell.textLabel.text = brotherCell.name;
    NSLog(@"BrotherCell Name = %@", brotherCell.name);

//    NSDate *object = _objects[indexPath.row];
//    cell.textLabel.text = [object description];
    return cell;
}

这是我的MPTBrothers.m

#import "MPTBrothers.h"

@implementation MPTBrothers

-(id)initWithInfo:(NSString *)name age:(int)age height:(int)height {
    if (self = [self init])
    _name = name;
    _age = age;
    _height = height;

    return self;
}

0 个答案:

没有答案