iphone - iOS 版 Facebook API 中的分析器警告

标签 iphone objective-c ios facebook analyzer

这是 APICallsViewController 文件中的代码和几个屏幕截图。我的应用程序崩溃了,我不确定这是否是原因。

谢谢你的帮助

enter image description here

 // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UILabel *textLabel;
    UILabel *detailTextLabel;
    UIButton *button;

    UIFont *cellFont = [UIFont boldSystemFontOfSize:14.0];
    UIFont *detailCellFont = [UIFont fontWithName:@"Helvetica" size:12.0];

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        // Initialize API title UILabel
        textLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
        textLabel.tag = TITLE_TAG;
        textLabel.font = cellFont;
        [cell.contentView addSubview:textLabel];

        // Initialize API description UILabel
        detailTextLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        detailTextLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
        detailTextLabel.tag = DESCRIPTION_TAG;
        detailTextLabel.font = detailCellFont;
        detailTextLabel.textColor = [UIColor darkGrayColor];
        detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
        detailTextLabel.numberOfLines = 0;
        [cell.contentView addSubview:detailTextLabel];

        // Initialize API button UIButton
        button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
        [button setBackgroundImage:[[UIImage imageNamed:@"MenuButton.png"]
                                    stretchableImageWithLeftCapWidth:9 topCapHeight:9]
                          forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(apiButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];
    } else {
        textLabel = (UILabel *)[cell.contentView viewWithTag:TITLE_TAG];
        detailTextLabel = (UILabel *)[cell.contentView viewWithTag:DESCRIPTION_TAG];
        // For the button cannot search by tag since it is not constant
        // and is dynamically used figure out which button is clicked.
        // So instead we loop through subviews of the cell to find the button.
        for (UIView *subview in cell.contentView.subviews) {
            if([subview isKindOfClass:[UIButton class]]) {
                button = (UIButton *)subview;
            }
        }
    }

    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);

    // The API title
    NSString *cellText = [[apiMenuItems objectAtIndex:indexPath.row] objectForKey:@"title"];
    CGSize labelSize = [cellText sizeWithFont:cellFont
                            constrainedToSize:constraintSize
                                lineBreakMode:UILineBreakModeWordWrap];
    textLabel.frame = CGRectMake(20, 2,
                                  (cell.contentView.frame.size.width-40),
                                  labelSize.height);
    textLabel.text = cellText;

    // The API description
    NSString *detailCellText = [[apiMenuItems objectAtIndex:indexPath.row] objectForKey:@"description"];
    CGSize detailLabelSize = [detailCellText sizeWithFont:detailCellFont
                                        constrainedToSize:constraintSize
                                            lineBreakMode:UILineBreakModeWordWrap];
    detailTextLabel.frame = CGRectMake(20, (labelSize.height + 4),
                                       (cell.contentView.frame.size.width-40),
                                       detailLabelSize.height);
    detailTextLabel.text = detailCellText;


    // The API button
    CGFloat yButtonOffset = labelSize.height + detailLabelSize.height + 15;
    button.frame = CGRectMake(20, yButtonOffset, (cell.contentView.frame.size.width-40), 44);
    [button setTitle:[[apiMenuItems objectAtIndex:indexPath.row] objectForKey:@"button"]
            forState:UIControlStateNormal];
    // Set the tag that will later identify the button that is clicked.
    button.tag = indexPath.row;


    return cell;
}

最佳答案

button 变量是一个局部变量。除非你使用的是 ARC,否则不能保证它被初始化为任何东西。这可能是无稽之谈。分析器警告您,当您尝试向它发送消息时,它是否已经分配了一个有意义的地址尚不清楚(frame 属性的 setter 是您发送的消息) .

考虑一下:cell 不是 nil,并且您在其 subview 中找不到按钮。如果是这种情况,应用程序将到达有问题的行,而无需分配给 button 变量。它可以包含指向绝对任何地方的指针。如果您尝试取消引用指针以向对象发送消息,您可能会在应用程序的任何位置涂鸦内存。

现在,您可能知道一定会找到作为按钮的子类。但分析器无法弄清楚这一点。所以无论如何它都会警告你。

您应该将button 变量初始化为nil。如果有可能找不到按钮,你应该处理这种情况。

关于iphone - iOS 版 Facebook API 中的分析器警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10624271/

相关文章:

iphone - 在 iOS WebView 中添加 Facebook 的 Like 按钮

iphone - iOS PhotoPicker 示例在 4.0 模拟器下崩溃

iOS:SQLITE 插入查询错误

objective-c - 等待任务时忽略用户输入 - Objective-C

ios - 适用于 iOS 和 macOS 的 Swift 框架

iphone - 如何在iPhone应用程序中将搜索索引添加到sqlite数据库

ios - 在 danielgindi/Charts ios 中显示真实的 x 轴值

ios - Swift - 预期声明在 'View Controller' 的声明中

ios - subview 有委托(delegate),糟糕的设计?

iphone - 将 iOS (iPhone) 应用移植到 Mac?