得到此警告初始化类型' id'

时间:2014-11-25 07:58:41

标签: ios objective-c

    - (id) firstResponder: (UIView *) navView {
            for (id aview in [self allSubviews:navView]) {
                if ([aview isKindOfClass:[UITextField class]] && [(UITextField *)aview isFirstResponder]) {
                    return (UITextField *)aview;
            }
            else if ([aview isKindOfClass:[UITextView class]] && [(UITextView *)aview isFirstResponder]) {
                return (UITextView *)aview;
            }
        }
        return NO;
    }

收到错误:从iOS 8中的常量布尔表达式初始化类型'id'的指针为null。

2 个答案:

答案 0 :(得分:6)

变化:

return NO;

为:

return nil;

NO是一个布尔文字,编译器抱怨它被转换为类型id以便返回。)

答案 1 :(得分:0)

如何更改实现如下...

在此,您无需担心NOnilnull等,即使编译器不会警告idinteger

- (id) firstResponder: (UIView *) navView {
    UIView *view = nil;
    for (id aview in [self allSubviews:navView]) {
        if ([aview isKindOfClass:[UITextField class]] && [(UITextField *)aview isFirstResponder]) {
            view = (UITextField *)aview;
        }
        else if ([aview isKindOfClass:[UITextView class]] && [(UITextView *)aview isFirstResponder]) {
            view = (UITextView *)aview;
        }
    }
    return view;
}