2

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

整个代码在这里:

视图控制器.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

视图控制器.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

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

4

5 回答 5

5

不要这样做:

tableItems = [tableItems addObject:timeString];

这样做:

[tableItems addObject:timeString];
于 2012-11-01T02:58:01.113 回答
4

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

tableItems = [tableItems addObject:timeString];

至:

[tableItems addObject:timeString];

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

于 2012-11-01T02:57:08.477 回答
1

这条线

tableItems = [tableItems addObject:timeString];

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

编译器的错误在这里是一个提示:它无法弄清楚你为什么将void(返回值;什么都没有)分配给它知道应该是NSMutableArray.

于 2012-11-01T02:56:50.350 回答
1

改变

tableItems = [tableItems addObject:timeString];

[tableItems addObject:timeString];

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

于 2012-11-01T02:57:42.707 回答
0

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

NSMutableArray *anArray = [NSMutableArray array];

接下来,以下行:

tableItems = [tableItems addObject:timeString];

应该:

[tableItems addObject:timeString];

没有必要重新声明它。

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

于 2012-11-01T02:57:35.530 回答