属性 'preferredDatePickerStyle' 未在 'UIDatePicker' 类型的对象上找到

Property 'preferredDatePickerStyle' not found on object of type 'UIDatePicker'

我正在使用 react-native 0.62.2 版本和 datetimepicker 版本 3.0.1。当我尝试使用 xcode 构建项目时,显示了以下错误:

  1. 使用未声明的标识符'UIDatePickerStyleCompact'
  2. 使用未声明的标识符'UIDatePickerStyleWheels'
  3. 属性 'preferredDatePickerStyle' 未在类型对象上找到 'RNDateTimePicker *'
  4. 属性 'preferredDatePickerStyle' 未在类型对象上找到 'UIDatePicker*'
  5. 'NSInteger'(又名'long')到'id'的隐式转换是 不允许使用 ARC
  6. 语句需要整数类型的表达式('__strongid' 无效)
  7. 'UIDatePickerMode' 的隐式转换(又名“枚举” UIDatePickerMode') 到 'id' 在 ARC
  8. 中是不允许的
  9. 使用未声明的标识符'UIDatePickerStyleWheels'

下面是里面的代码RNDateTimePickerManager.h

#import "RNDateTimePickerManager.h"

#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import "RNDateTimePicker.h"
#import <React/UIView+React.h>

@implementation RCTConvert(UIDatePicker)

RCT_ENUM_CONVERTER(UIDatePickerMode, (@{
  @"time": @(UIDatePickerModeTime),
  @"date": @(UIDatePickerModeDate),
  @"datetime": @(UIDatePickerModeDateAndTime),
}), UIDatePickerModeTime, integerValue)

RCT_ENUM_CONVERTER(UIDatePickerStyle, (@{                //Error 5
    @"default": @(UIActionSheetStyleAutomatic),
    @"compact": @(UIDatePickerStyleCompact),             //Error 1
    @"spinner": @(UIDatePickerStyleWheels),              //Error 2
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000
    @"inline": @(UIDatePickerStyleInline),
#endif
}), UIActionSheetStyleAutomatic, integerValue)

@end

@implementation RNDateTimePickerManager

RCT_EXPORT_MODULE()

- (UIView *)view
{
  return [RNDateTimePicker new];
}

+ (NSString*) datepickerStyleToString: (UIDatePickerStyle) style {
    // RCTConvert does not handle this.?
    switch (style) {                                       //Error 6
        case UIDatePickerStyleCompact:
            return @"compact";
        case UIDatePickerStyleWheels:
            return @"spinner";
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000
        case UIDatePickerStyleInline:
            return @"inline";
#endif
        default:
            [NSException raise:@"Unsupported style value" format:@"UIDatePickerStyle of %ld is unsupported", (long)style];
            return @"";
    }
}

RCT_EXPORT_METHOD(getDefaultDisplayValue:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIDatePicker* view = [RNDateTimePicker new];
        
        view.preferredDatePickerStyle = UIActionSheetStyleAutomatic;          //Error 4
        UIDatePickerMode renderedMode = [RCTConvert UIDatePickerMode:options[@"mode"]];
        view.datePickerMode = renderedMode;
        // NOTE afaict we do not need to measure the actual dimensions here, but if we do, just look at the original PR
        
        UIDatePickerMode determinedDisplayValue = view.datePickerMode;    //Error 7

        resolve(@{
                 @"determinedDisplayValue": [RNDateTimePickerManager datepickerStyleToString:determinedDisplayValue],
                });
    });
}

RCT_EXPORT_VIEW_PROPERTY(date, NSDate)
RCT_EXPORT_VIEW_PROPERTY(locale, NSLocale)
RCT_EXPORT_VIEW_PROPERTY(minimumDate, NSDate)
RCT_EXPORT_VIEW_PROPERTY(maximumDate, NSDate)
RCT_EXPORT_VIEW_PROPERTY(minuteInterval, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
RCT_REMAP_VIEW_PROPERTY(mode, datePickerMode, UIDatePickerMode)
RCT_REMAP_VIEW_PROPERTY(timeZoneOffsetInMinutes, timeZone, NSTimeZone)

RCT_CUSTOM_VIEW_PROPERTY(textColor, UIColor, RNDateTimePicker)
{
    if (@available(iOS 14.0, *) && view.datePickerMode != UIDatePickerStyleWheels) {  //Error 8
    // prevents #247
    return;
  }
  if (json) {
    [view setValue:[RCTConvert UIColor:json] forKey:@"textColor"];
    [view setValue:@(NO) forKey:@"highlightsToday"];
  } else {
    UIColor* defaultColor;
    if (@available(iOS 13.0, *)) {
        defaultColor = [UIColor labelColor];
    } else {
        defaultColor = [UIColor blackColor];
    }
    [view setValue:defaultColor forKey:@"textColor"];
    [view setValue:@(YES) forKey:@"highlightsToday"];
  }
}

// TODO vonovak setting preferredDatePickerStyle invalidates minuteinterval
RCT_CUSTOM_VIEW_PROPERTY(displayIOS, UIDatePickerStyle, RNDateTimePicker)
{
    if (@available(iOS 13.4, *)) {
        if (json) {
            UIDatePickerMode propValue = [RCTConvert UIDatePickerStyle:json];
            view.preferredDatePickerStyle = propValue;           //Error 3
        } else {
            view.preferredDatePickerStyle = UIActionSheetStyleAutomatic;
        }
    }
}

@end

react-native datetimepicker有bug吗?

你可以使用最新的稳定版 XCode (11.6)。

对于那些仍然坚持这一点的人,特别是使用 this 库。

从版本 3.0.3 开始,它要求您的 Xcode 至少是 v11,但他们从未提到 v11.3 将无法工作,很多人已经注意到您至少需要 v11 .6.

请注意,降级可能也没有真正帮助你,因为你错过了解决很多错误的更新点,例如 #217 fix

对此的最佳答案是将 xcode 至少更新到 v11.6。我已经解决了超过 48 小时的问题,当应用程序成功构建结束时,我与缺少的依赖项作斗争。