将字符串对象添加到NSMutableArray

时间:2012-11-01 02:52:34

标签: objective-c ios nsmutablearray

我正在尝试将NSString对象添加到NSMutuableArray:tableItems = [tableItems addObject:timeString];。我已经初始化了数组但是当我添加NSString对象时,我收到此错误:Assigning to 'NSMutuableArray *_strong'from incompatible type void.

整个代码在这里:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    UILabel *lbl;
    NSTimer *stopTimer;
    NSDate *startDate;
    BOOL running,lap;
    UIButton *bttn;
    NSMutableArray *tableItems;
    NSString *timeString;
}

@property (strong,nonatomic) IBOutlet UILabel *lbl;
@property (strong,nonatomic) IBOutlet UIButton *bttn;
@property (strong,nonatomic) NSMutableArray *tableItems;
@property (strong,nonatomic) NSString *timeString;

-(IBAction)startPressed:(id)sender;
-(IBAction)resetPressed:(id)sender;

-(void)updateTimer;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize lbl,bttn,tableItems,timeString;

- (void)viewDidLoad
{
    [super viewDidLoad];
    lbl.text = @"00.00.00.000";
    running = FALSE;
    lap = FALSE;
    startDate = [NSDate date];
    tableItems = [[NSMutableArray alloc] init];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)startPressed:(id)sender{
    if(!running){
        running = TRUE;
        lap = TRUE;
        [sender setTitle:@"Stop" forState:UIControlStateNormal];
        [bttn setTitle:@"Lap" forState:UIControlStateNormal];
        if (stopTimer == nil) {
            stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                         target:self
                                                       selector:@selector(updateTimer)
                                                       userInfo:nil
                                                        repeats:YES];
        }
    }else{
        running = FALSE;
        lap = FALSE;
        [sender setTitle:@"Start" forState:UIControlStateNormal];
        [bttn setTitle:@"Restart" forState:UIControlStateNormal];
        [stopTimer invalidate];
        stopTimer = nil;
    }

}
-(void)updateTimer{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    timeString=[dateFormatter stringFromDate:timerDate];
    lbl.text = timeString;
}

-(IBAction)resetPressed:(id)sender{
    if (!lap) {
        [stopTimer invalidate];
        stopTimer = nil;
        startDate = [NSDate date];
        lbl.text = @"00.00.00.000";
        running = FALSE;
    }
    else{
        tableItems = [tableItems addObject:timeString];
    }

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return tableItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //Step 1:Check whether if we can reuse a cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    //If there are no new cells to reuse,create a new one
    if(cell ==  nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        //UIView *v = [[UIView alloc] init];
        //v.backgroundColor = [UIColor redColor];
        //cell.selectedBackgroundView = v;
        //changing the radius of the corners
        //cell.layer.cornerRadius = 10;

    }

    //Set the image in the row
    //cell.imageView.image = [images objectAtIndex:indexPath.row];

    //Step 3: Set the cell text content
    cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];

    //Step 4: Return the row
    return cell;

}


@end

不确定我的错误是什么。需要一些指导。对不起,如果这是一个愚蠢的问题。

5 个答案:

答案 0 :(得分:5)

不要这样做:

tableItems = [tableItems addObject:timeString];

做到这一点:

[tableItems addObject:timeString];

答案 1 :(得分:4)

由于这是一个可变数组,因此请更改:

tableItems = [tableItems addObject:timeString];

为:

[tableItems addObject:timeString];

addObject方法声明为:- (void)addObject:(id)anObject,(如here所示),并且由于返回类型为void,因此不会返回任何内容。

答案 2 :(得分:1)

这一行

tableItems = [tableItems addObject:timeString];

并不完全按照你的想法行事。 tableItems是一个可变数组,您只想将时间字符串添加到它。您不需要将返回值重新分配给任何内容,因为此方法会添加该项并且不返回任何内容(void)。

编译器的错误在这里提示:它无法弄清楚为什么要将void(返回值;没有)分配给它知道应该是{{1}的属性}。

答案 3 :(得分:1)

更改

tableItems = [tableItems addObject:timeString];

[tableItems addObject:timeString];

你不必分配它。 [tableItems addObject:timeString];的返回类型为void,您无法将其分配给NSArray对象。这就是编译器所说的。

答案 4 :(得分:0)

首先,您应该像这样初始化数组。

NSMutableArray *anArray = [NSMutableArray array];

接下来,以下一行:

tableItems = [tableItems addObject:timeString];

应该是:

[tableItems addObject:timeString];

没有必要重新声明它。

最后,你确定timeString不是空的吗?