ARC 不允许将 'int' 隐式转换为 'UILabel *'

Implicit conversion of 'int' to 'UILabel *' is disallowed with ARC

我在下面的代码中遇到编译错误 "Implicit conversion of 'int' to 'UILabel *' is disallowed with ARC"。

UILabel* (^makeLabel)(CGFloat, CGFloat, CGFloat, CGFloat, NSString*) = ^(CGFloat x, CGFloat y, CGFloat w, CGFloat h, NSString* title) {
    UILabel* label = [[UILabel alloc] init];
    label.frame = CGRectMake(x, y, w, h);
    label.text = title;
    label.font = [UIFont systemFontOfSize:14];
    return label;
};

UILabel* lblEmail = mekeLabel(30, 100, 100, 30, @"이메일");

我应该怎么做才能解决这个问题?

您的代码中有错字。你应该打电话给 makeLabel(30, 100, 100, 30, @"이메일");。出现此错误的原因是 mekeLabel 被视为稍后声明的块。默认情况下 Objective-C return int 中的块。

您需要在任何地方明确定义 return 类型,例如:

UILabel * (^makeLabel)(CGFloat, CGFloat, CGFloat, CGFloat, NSString *) = ^UILabel *(CGFloat x, CGFloat y, CGFloat w, CGFloat h, NSString * title) {
    UILabel * label = [[UILabel alloc] init];
    label.frame = CGRectMake(x, y, w, h);
    label.text = title;
    label.font = [UIFont systemFontOfSize:14];
    return label;
};

那么这将完美运行:

UILabel * lblEmail = makeLabel(30, 100, 100, 30, @"이메일");