【问题标题】:Adding a string object to NSMutuableArray将字符串对象添加到 NSMutableArray
【发布时间】:2012-10-21 15:19:50
【问题描述】:

我正在尝试将 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

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

【问题讨论】:

    标签: objective-c ios nsmutablearray


    【解决方案1】:

    不要这样做:

    tableItems = [tableItems addObject:timeString];
    

    这样做:

    [tableItems addObject:timeString];
    

    【讨论】:

      【解决方案2】:

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

      tableItems = [tableItems addObject:timeString];
      

      到:

      [tableItems addObject:timeString];
      

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

      【讨论】:

        【解决方案3】:

        这一行

        tableItems = [tableItems addObject:timeString];
        

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

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

        【讨论】:

        • 编译器错误消失了,但我仍然无法将时间字符串添加到我的 tableview。知道错误可能是什么吗?
        【解决方案4】:

        改变

        tableItems = [tableItems addObject:timeString];
        

        到

        [tableItems addObject:timeString];
        

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

        【讨论】:

          【解决方案5】:

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

          NSMutableArray *anArray = [NSMutableArray array];

          接下来,下面一行:

          tableItems = [tableItems addObject:timeString];

          应该是:

          [tableItems addObject:timeString];

          无需重新声明。

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

          【讨论】:

          • 为什么要这样初始化一个数组?我不相信你是正确的..我指的是回答者,而不是上面的评论
          • 推理:stackoverflow.com/questions/5423211/… 对于 ARC,它并不重要,但在过去,它与内存管理有关。这些类型的初始化器创建自动释放的对象。这是一个更安全的初始化程序,根据我的经验,如果您通过发送array 消息初始化NSArray/NSMutableArray,并初始化NSDictionary/NSMutableDictionary 发送dictionary 消息,这是“最佳实践”。
          • 编辑了我的第二条评论/回复,因此请务必重新阅读。
          • 我不认为在 ARC 下使用一种方法比另一种方法更安全或在其他方面具有实质性优势。 (在这种情况下,由于分配给 ivar,ARC 之前的形式更可能是 alloc/init。)好奇为什么这对您来说是最佳实践?
          • 我认为这在我为 iOS 4 开发的日子里更多的是出于习惯。当时,我正在使用 array 来自动初始化一个新的自动释放对象。我认为这些天(例如,在 ARC 下),这并不重要。我只是坚持惯例。我想直到你提出来我才考虑过,Ben,
          猜你喜欢
          • 1970-01-01
          • 2012-11-29
          • 1970-01-01
          • 2012-01-25
          • 1970-01-01
          • 1970-01-01
          • 2011-08-22
          • 1970-01-01
          相关资源
          最近更新 更多