解析iOS XCode:类名不能以下划线开头

问题描述 投票:0回答:1

我在 Parse 初始化时收到此错误。 很可能是因为我更新到了 XCode 版本 16.0 (16A242d)。 MacOS 是 14.5 (23F79)。

我已将 Parse 安装为 pod。还尝试删除它并安装最新版本作为包依赖项,但同样的错误。

[Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
            configuration.applicationId = @"myAppId";
            configuration.clientKey = nil;
            configuration.server = @"myParseServerUrlStringHostedonHeroku";
}]];

这是我运行应用程序时得到的结果:(编译时没有错误。)

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: Invalid class name. Class names cannot start with an underscore.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001804ae0f8 __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x0000000180087db4 objc_exception_throw + 56
    2   CoreFoundation                      0x00000001804ae008 -[NSException initWithCoder:] + 0
    3   myApp.debug.dylib   0x0000000106d2f1f8 +[PFObject(Private) _assertValidInstanceClassName:] + 180
    4   myApp.debug.dylib   0x0000000106d37598 -[PFObject init] + 436
    5   myApp.debug.dylib   0x0000000106d37898 -[PFObject initWithObjectState:] + 92
    6   myApp.debug.dylib   0x0000000106d379cc +[PFObject objectWithClassName:objectId:completeData:] + 248
    7   myApp.debug.dylib   0x0000000106d37ae0 +[PFObject objectWithClassName:] + 80
    8   myApp.debug.dylib   0x0000000106d37fcc +[PFObject object] + 252
    9   myApp.debug.dylib   0x0000000106cf6bdc __56-[PFCurrentInstallationController getCurrentObjectAsync]_block_invoke.9 + 452
    10  myApp.debug.dylib   0x0000000106a64f18 __62-[BFTask continueWithExecutor:successBlock:cancellationToken:]_block_invoke + 124
    11  myApp.debug.dylib   0x0000000106a64848 __55-[BFTask continueWithExecutor:block:cancellationToken:]_block_invoke + 88
    12  myApp.debug.dylib   0x0000000106a615c0 __29+[BFExecutor defaultExecutor]_block_invoke_2 + 152
    13  myApp.debug.dylib   0x0000000106a61da0 -[BFExecutor execute:] + 88
    14  myApp.debug.dylib   0x0000000106a64b24 __55-[BFTask continueWithExecutor:block:cancellationToken:]_block_invoke.15 + 44
    15  myApp.debug.dylib   0x0000000106a642f4 -[BFTask runContinuations] + 508
    16  myApp.debug.dylib   0x0000000106a63cd8 -[BFTask trySetResult:] + 208
    17  myApp.debug.dylib   0x0000000106a65a50 -[BFTaskCompletionSource trySetResult:] + 88
    18  myApp.debug.dylib   0x0000000106ce88fc __28-[PFAsyncTaskQueue enqueue:]_block_invoke_2 + 216
    19  myApp.debug.dylib   0x0000000106a64848 __55-[BFTask continueWithExecutor:block:cancellationToken:]_block_invoke + 88
    20  libdispatch.dylib                   0x00000001022cbec4 _dispatch_call_block_and_release + 24
    21  libdispatch.dylib                   0x00000001022cd73c _dispatch_client_callout + 16
    22  libdispatch.dylib                   0x00000001022d05a0 _dispatch_queue_override_invoke + 1320
    23  libdispatch.dylib                   0x00000001022e136c _dispatch_root_queue_drain + 372
    24  libdispatch.dylib                   0x00000001022e1f18 _dispatch_worker_thread2 + 256
    25  libsystem_pthread.dylib             0x00000001022277d8 _pthread_wqthread + 224
    26  libsystem_pthread.dylib             0x00000001022265d4 start_wqthread + 8

调试后,这是造成问题的原因: 在 PFObject 中:

+ (instancetype)object {
    PFConsistencyAssert([self conformsToProtocol:@protocol(PFSubclassing)],
                        @"Can only call +object on subclasses conforming to PFSubclassing");
    NSString *className = [(id<PFSubclassing>)self parseClassName];
    Class class = [[self subclassingController] subclassForParseClassName:className] ?: [PFObject class];
    return [class objectWithClassName:className];
}

NSString *className = [(id<PFSubclassing>)self parseClassName];
在这一行中,className 是“_Installation”,因为在 PFInstallation 中有:

+ (NSString *)parseClassName {
    return @"_Installation";
}

此外,

[[self subclassingController] subclassForParseClassName:className]
返回零。

几乎一整天都在这上面,没有休息,也没有沮丧。我唯一怀疑的是子类没有在 Parse->Core->PFObjectSubclassingController 中注册:

- (void)registerSubclass:(Class<PFSubclassing>)kls {
    pf_sync_with_throw(_registeredSubclassesAccessQueue, ^{
        [self _rawRegisterSubclass:kls];
    });
}

[[self subclassingController] subclassForParseClassName:className]
处给出零。

请帮忙!急需。谢谢!

ios objective-c xcode parse-platform
1个回答
0
投票

我们遇到了同样的问题。简单的解决方法是在应用程序启动时注册您的 PFObject 类。

在 macOS Swift 上,可以在此处完成。

func applicationDidFinishLaunching(_ aNotification: Notification) {
    // Insert code here to initialize your application

    PFInstallation.registerSubclass()
    PFUser.registerSubclass()
    PFSession.registerSubclass()
}

我们发现在这里做更方便:

override func viewDidLoad() {
    super.viewDidLoad()

    let configuration = ParseClientConfiguration {
        $0.applicationId = Constants.PARSE_APPLICATION_ID
        $0.clientKey = Constants.PARSE_CLIENT_KEY
        $0.server = Constants.PARSE_SERVER
    }

    Parse.initialize(with: configuration)
    
    PFInstallation.registerSubclass()
    PFUser.registerSubclass()
    PFSession.registerSubclass()
}
© www.soinside.com 2019 - 2024. All rights reserved.