2

I am trying to add a NSString object to NSMutuableArray: tableItems = [tableItems addObject:timeString];. I have already initialised the array but when I add the NSString object, I get this error: Assigning to 'NSMutuableArray *_strong'from incompatible type void.

The whole code is here:

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

Not sure what my error is. Need some guidance. Sorry if this is a stupid question.

iDev
  • 23,310
  • 7
  • 60
  • 85
lakshmen
  • 28,346
  • 66
  • 178
  • 276

5 Answers5

5

don't do:

tableItems = [tableItems addObject:timeString];

do just this:

[tableItems addObject:timeString];
Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
4

Since this is a mutable array, change:

tableItems = [tableItems addObject:timeString];

to:

[tableItems addObject:timeString];

The addObject method is declared as: - (void)addObject:(id)anObject, (as shown here) and since the return type is void it doesn't return anything.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
1

This line

tableItems = [tableItems addObject:timeString];

doesn't quite do what you think. tableItems is a mutable array, you just want to add the time string to it. You don't need to reassign the return value to anything, because this method adds the item and returns nothing (void).

The error from the compiler was a hint here: it couldn't figure out why you were assigning void (the return value; nothing) to a property that it knew was supposed to be an NSMutableArray.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • the compiler error is gone but still i am not able to add timestring to my tableview.any idea what the error might be? – lakshmen Nov 01 '12 at 03:22
1

Change

tableItems = [tableItems addObject:timeString];

to

[tableItems addObject:timeString];

You dont have to assign it. [tableItems addObject:timeString]; is of return type void and you cant assign it to an NSArray object. That's what compiler is saying.

iDev
  • 23,310
  • 7
  • 60
  • 85
0

First, you should always initialize your arrays like so.

NSMutableArray *anArray = [NSMutableArray array];

Next, the following line:

tableItems = [tableItems addObject:timeString];

should be:

[tableItems addObject:timeString];

There's no need to redeclare it.

Finally, are you sure that timeString isn't empty?

ArtSabintsev
  • 5,170
  • 10
  • 41
  • 71
  • Why would you initialize an array like that? I don't believe you're correct..And im referring the answerer, not the comment above – Tommy Devoy Nov 01 '12 at 02:59
  • Reasoning here: http://stackoverflow.com/questions/5423211/difference-between-nsmutablearray-array-vs-nsmutablearray-alloc-init With ARC, it's not really important, but in the past, it had to do with memory management. These type of initializers create auto-released objects. It's a safer initializer, and in my experience, it's 'best practice' if you initialize `NSArray`/`NSMutableArray` by sending an `array` message, and initialize `NSDictionary`/`NSMutableDictionary` sending a `dictionary` message. – ArtSabintsev Nov 01 '12 at 03:03
  • Edited my second comment/response, so make sure to re-read it. – ArtSabintsev Nov 01 '12 at 03:05
  • I don't believe it's either safer or otherwise materially advantageous to use one method over another under ARC. (In this case since the assignment is to an ivar, the pre-ARC form would have more likely been the alloc/init.) Curious why this is stylistically a best practice for you? – Ben Zotto Nov 01 '12 at 05:43
  • I think it's more out of habit from my days developing for iOS 4. At the time, I was using `array` to automatically initialize a new auto-released object. I think these days (e.g., under ARC), it doesn't matter. I'm just stuck with the convention. I guess I haven't thought about it until you brought it up, Ben, – ArtSabintsev Nov 01 '12 at 18:16