indexPath.row-1是4294967295

时间:2011-09-12 19:40:21

标签: iphone objective-c ios xcode uitableview

我有一个indexPath.row,它是1并且记录1(当使用NSLog时)。如果我调用indexPath.row-1(应该返回0),则返回4294967295。

我正在尝试返回objectAtIndex:indexPath.row-1但是当我收到4294967295时。

有什么想法吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    Singleton *singleton = [Singleton sharedSingleton];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    if ([[prefs objectForKey:@"isYes"]boolValue] == 1 && randomMarker != 100)
    {
        //sets cell image
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,98,100)];
        imgView.image = [UIImage imageNamed:@"stackoverflow.png"];
        cell.imageView.image = imgView.image;

        //sets cell text
        cell.textLabel.text = @"Text";
        self.checkedInCount == 100;
    }
    else if ([[prefs objectForKey:@"isYes"]boolValue] == 1 && randomMarker == 100)
    {
        //gets cell and cleans up cell text
        NSLog(@"%@", indexPath.row);
        NSString *title = [[[singleton linkedList]objectAtIndex:(indexPath.row-1)]objectForKey:@"desc"];

2 个答案:

答案 0 :(得分:10)

当您尝试给unsigned int(NSUInteger)一个负值时,它通常会返回一个非常大的正值。

您正在致电

NSString *tempDesc = [[[singleton linkedList]objectAtIndex:indexPath.row-1]objectForKey:@"desc"]; 

indexPath.row的值为0时,翻译为:

NSString *tempDesc = [[[singleton linkedList]objectAtIndex:-1]objectForKey:@"desc"]; 

由于objectAtIndex:将无符号整数作为参数,-1将转换为垃圾值4294967295

要避免此问题,请先检查1为正数,否则不要从0中减去indexPath.row


这是另一个问题:

NSLog(@"%@", indexPath.row);

这应改为:

NSLog(@"%u", indexPath.row);

答案 1 :(得分:0)

NSLog(@"%@", indexPath.row);

您应该将%d用作整数作为indexPath.row将返回一个整数

使用NSLog(@“%d”,indexPath.row);