indexPath.row-1是4294967295

Bau*_*aub 0 iphone xcode objective-c uitableview ios

我有一个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"];
Run Code Online (Sandbox Code Playgroud)

Pen*_*One 10

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

你在打电话

NSString *tempDesc = [[[singleton linkedList]objectAtIndex:indexPath.row-1]objectForKey:@"desc"]; 
Run Code Online (Sandbox Code Playgroud)

什么时候indexPath.row有价值0,所以翻译是:

NSString *tempDesc = [[[singleton linkedList]objectAtIndex:-1]objectForKey:@"desc"]; 
Run Code Online (Sandbox Code Playgroud)

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

为了避免这个问题,不减去10先检查indexPath.row是积极的.


这是另一个问题:

NSLog(@"%@", indexPath.row);
Run Code Online (Sandbox Code Playgroud)

这应该是:

NSLog(@"%u", indexPath.row);
Run Code Online (Sandbox Code Playgroud)

  • @James我确定它只是评论中的轻松语法,但是当你说`indexPath> 0`时,你的意思是`indexPath.row> 0`对吗?无论如何,如果可以的话,发布实际代码,你可能会留下重要的东西. (2认同)