尝试使用前向类“NSManagedObjectModel”作为 Swift 类模型的超类

Posted

技术标签:

【中文标题】尝试使用前向类“NSManagedObjectModel”作为 Swift 类模型的超类【英文标题】:Attempting to use the forward class 'NSManagedObjectModel' as superclass of Swift Class model 【发布时间】:2020-08-15 10:32:24 【问题描述】:

我正在尝试为我的统一项目使用核心数据。在目标 c 项目中使用 swift 类以便于编码,所以我创建了一个桥接头

'SwiftPlugin-Swift.h' - 不可编辑的不可见头文件

// Generated by Apple Swift version 5.2.4 effective-4.1.50 (swiftlang-1103.0.32.9 clang-1103.0.32.53)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgcc-compat"

#if !defined(__has_include)
# define __has_include(x) 0
#endif
#if !defined(__has_attribute)
# define __has_attribute(x) 0
#endif
#if !defined(__has_feature)
# define __has_feature(x) 0
#endif
#if !defined(__has_warning)
# define __has_warning(x) 0
#endif

#if __has_include(<swift/objc-prologue.h>)
# include <swift/objc-prologue.h>
#endif

#pragma clang diagnostic ignored "-Wauto-import"
#include <Foundation/Foundation.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

#if !defined(SWIFT_TYPEDEFS)
# define SWIFT_TYPEDEFS 1
# if __has_include(<uchar.h>)
#  include <uchar.h>
# elif !defined(__cplusplus)
typedef uint_least16_t char16_t;
typedef uint_least32_t char32_t;
# endif
typedef float swift_float2  __attribute__((__ext_vector_type__(2)));
typedef float swift_float3  __attribute__((__ext_vector_type__(3)));
typedef float swift_float4  __attribute__((__ext_vector_type__(4)));
typedef double swift_double2  __attribute__((__ext_vector_type__(2)));
typedef double swift_double3  __attribute__((__ext_vector_type__(3)));
typedef double swift_double4  __attribute__((__ext_vector_type__(4)));
typedef int swift_int2  __attribute__((__ext_vector_type__(2)));
typedef int swift_int3  __attribute__((__ext_vector_type__(3)));
typedef int swift_int4  __attribute__((__ext_vector_type__(4)));
typedef unsigned int swift_uint2  __attribute__((__ext_vector_type__(2)));
typedef unsigned int swift_uint3  __attribute__((__ext_vector_type__(3)));
typedef unsigned int swift_uint4  __attribute__((__ext_vector_type__(4)));
#endif

#if !defined(SWIFT_PASTE)
# define SWIFT_PASTE_HELPER(x, y) x##y
# define SWIFT_PASTE(x, y) SWIFT_PASTE_HELPER(x, y)
#endif
#if !defined(SWIFT_METATYPE)
# define SWIFT_METATYPE(X) Class
#endif
#if !defined(SWIFT_CLASS_PROPERTY)
# if __has_feature(objc_class_property)
#  define SWIFT_CLASS_PROPERTY(...) __VA_ARGS__
# else
#  define SWIFT_CLASS_PROPERTY(...)
# endif
#endif

#if __has_attribute(objc_runtime_name)
# define SWIFT_RUNTIME_NAME(X) __attribute__((objc_runtime_name(X)))
#else
# define SWIFT_RUNTIME_NAME(X)
#endif
#if __has_attribute(swift_name)
# define SWIFT_COMPILE_NAME(X) __attribute__((swift_name(X)))
#else
# define SWIFT_COMPILE_NAME(X)
#endif
#if __has_attribute(objc_method_family)
# define SWIFT_METHOD_FAMILY(X) __attribute__((objc_method_family(X)))
#else
# define SWIFT_METHOD_FAMILY(X)
#endif
#if __has_attribute(noescape)
# define SWIFT_NOESCAPE __attribute__((noescape))
#else
# define SWIFT_NOESCAPE
#endif
#if __has_attribute(ns_consumed)
# define SWIFT_RELEASES_ARGUMENT __attribute__((ns_consumed))
#else
# define SWIFT_RELEASES_ARGUMENT
#endif
#if __has_attribute(warn_unused_result)
# define SWIFT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
# define SWIFT_WARN_UNUSED_RESULT
#endif
#if __has_attribute(noreturn)
# define SWIFT_NORETURN __attribute__((noreturn))
#else
# define SWIFT_NORETURN
#endif
#if !defined(SWIFT_CLASS_EXTRA)
# define SWIFT_CLASS_EXTRA
#endif
#if !defined(SWIFT_PROTOCOL_EXTRA)
# define SWIFT_PROTOCOL_EXTRA
#endif
#if !defined(SWIFT_ENUM_EXTRA)
# define SWIFT_ENUM_EXTRA
#endif
#if !defined(SWIFT_CLASS)
# if __has_attribute(objc_subclassing_restricted)
#  define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) SWIFT_CLASS_EXTRA
#  define SWIFT_CLASS_NAMED(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA
# else
#  define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA
#  define SWIFT_CLASS_NAMED(SWIFT_NAME) SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA
# endif
#endif
#if !defined(SWIFT_RESILIENT_CLASS)
# if __has_attribute(objc_class_stub)
#  define SWIFT_RESILIENT_CLASS(SWIFT_NAME) SWIFT_CLASS(SWIFT_NAME) __attribute__((objc_class_stub))
#  define SWIFT_RESILIENT_CLASS_NAMED(SWIFT_NAME) __attribute__((objc_class_stub)) SWIFT_CLASS_NAMED(SWIFT_NAME)
# else
#  define SWIFT_RESILIENT_CLASS(SWIFT_NAME) SWIFT_CLASS(SWIFT_NAME)
#  define SWIFT_RESILIENT_CLASS_NAMED(SWIFT_NAME) SWIFT_CLASS_NAMED(SWIFT_NAME)
# endif
#endif

#if !defined(SWIFT_PROTOCOL)
# define SWIFT_PROTOCOL(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_PROTOCOL_EXTRA
# define SWIFT_PROTOCOL_NAMED(SWIFT_NAME) SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_PROTOCOL_EXTRA
#endif

#if !defined(SWIFT_EXTENSION)
# define SWIFT_EXTENSION(M) SWIFT_PASTE(M##_Swift_, __LINE__)
#endif

#if !defined(OBJC_DESIGNATED_INITIALIZER)
# if __has_attribute(objc_designated_initializer)
#  define OBJC_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer))
# else
#  define OBJC_DESIGNATED_INITIALIZER
# endif
#endif
#if !defined(SWIFT_ENUM_ATTR)
# if defined(__has_attribute) && __has_attribute(enum_extensibility)
#  define SWIFT_ENUM_ATTR(_extensibility) __attribute__((enum_extensibility(_extensibility)))
# else
#  define SWIFT_ENUM_ATTR(_extensibility)
# endif
#endif
#if !defined(SWIFT_ENUM)
# define SWIFT_ENUM(_type, _name, _extensibility) enum _name : _type _name; enum SWIFT_ENUM_ATTR(_extensibility) SWIFT_ENUM_EXTRA _name : _type
# if __has_feature(generalized_swift_name)
#  define SWIFT_ENUM_NAMED(_type, _name, SWIFT_NAME, _extensibility) enum _name : _type _name SWIFT_COMPILE_NAME(SWIFT_NAME); enum SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_ENUM_ATTR(_extensibility) SWIFT_ENUM_EXTRA _name : _type
# else
#  define SWIFT_ENUM_NAMED(_type, _name, SWIFT_NAME, _extensibility) SWIFT_ENUM(_type, _name, _extensibility)
# endif
#endif
#if !defined(SWIFT_UNAVAILABLE)
# define SWIFT_UNAVAILABLE __attribute__((unavailable))
#endif
#if !defined(SWIFT_UNAVAILABLE_MSG)
# define SWIFT_UNAVAILABLE_MSG(msg) __attribute__((unavailable(msg)))
#endif
#if !defined(SWIFT_AVAILABILITY)
# define SWIFT_AVAILABILITY(plat, ...) __attribute__((availability(plat, __VA_ARGS__)))
#endif
#if !defined(SWIFT_WEAK_IMPORT)
# define SWIFT_WEAK_IMPORT __attribute__((weak_import))
#endif
#if !defined(SWIFT_DEPRECATED)
# define SWIFT_DEPRECATED __attribute__((deprecated))
#endif
#if !defined(SWIFT_DEPRECATED_MSG)
# define SWIFT_DEPRECATED_MSG(...) __attribute__((deprecated(__VA_ARGS__)))
#endif
#if __has_feature(attribute_diagnose_if_objc)
# define SWIFT_DEPRECATED_OBJC(Msg) __attribute__((diagnose_if(1, Msg, "warning")))
#else
# define SWIFT_DEPRECATED_OBJC(Msg) SWIFT_DEPRECATED_MSG(Msg)
#endif
#if !defined(IBSegueAction)
# define IBSegueAction
#endif
#if __has_feature(modules)
#if __has_warning("-Watimport-in-framework-header")
#pragma clang diagnostic ignored "-Watimport-in-framework-header"
#endif
@import CoreData;
@import Foundation;
@import ObjectiveC;
@import UIKit;
#endif

#pragma clang diagnostic ignored "-Wproperty-attribute-mismatch"
#pragma clang diagnostic ignored "-Wduplicate-method-arg"
#if __has_warning("-Wpragma-clang-attribute")
# pragma clang diagnostic ignored "-Wpragma-clang-attribute"
#endif
#pragma clang diagnostic ignored "-Wunknown-pragmas"
#pragma clang diagnostic ignored "-Wnullability"

#if __has_attribute(external_source_symbol)
# pragma push_macro("any")
# undef any
# pragma clang attribute push(__attribute__((external_source_symbol(language="Swift", defined_in="k3us",generated_declaration))), apply_to=any(function,enum,objc_interface,objc_category,objc_protocol))
# pragma pop_macro("any")
#endif

@class NSEntityDescription;
@class NSManagedObjectContext;

SWIFT_CLASS("_TtC4k3us10EventModel")
@interface EventModel : NSManagedObject
- (nonnull instancetype)initWithEntity:(NSEntityDescription * _Nonnull)entity insertIntoManagedObjectContext:(NSManagedObjectContext * _Nullable)context OBJC_DESIGNATED_INITIALIZER;
@end


@interface EventModel (SWIFT_EXTENSION(k3us))
@property (nonatomic) int64_t app_id;
@property (nonatomic, copy) NSString * _Nullable app_session_id;
@property (nonatomic, copy) NSString * _Nullable app_ver;
@property (nonatomic, copy) NSString * _Nullable counter;
@property (nonatomic, copy) NSString * _Nullable date;
@property (nonatomic, copy) NSString * _Nullable device_id;
@property (nonatomic, copy) NSString * _Nullable device_info;
@property (nonatomic) int64_t event_id;
@property (nonatomic, copy) NSString * _Nullable genus;
@property (nonatomic, copy) NSString * _Nullable kingdom;
@property (nonatomic, copy, getter=module, setter=setModule:) NSString * _Nullable module_;
@property (nonatomic, copy) NSString * _Nullable network_info;
@property (nonatomic, copy) NSString * _Nullable phylum;
@property (nonatomic, copy) NSString * _Nullable priority;
@property (nonatomic, copy) NSString * _Nullable screen_session_id;
@property (nonatomic) int64_t u_event_id;
@property (nonatomic) int64_t u_ref_event_id;
@property (nonatomic) int64_t user_id;
@property (nonatomic) int64_t value2;
@end


SWIFT_CLASS("_TtC4k3us18SQLiteHelperObject")
@interface SQLiteHelperObject : NSObject
- (void)trackEventWithJson:(NSString * _Nonnull)json;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

@class UNNotificationRequest;

SWIFT_CLASS("_TtC4k3us19TNLSalesForceBridge")
@interface TNLSalesForceBridge : NSObject
SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly, strong) TNLSalesForceBridge * _Nonnull shared;)
+ (TNLSalesForceBridge * _Nonnull)shared SWIFT_WARN_UNUSED_RESULT;
- (BOOL)setContactKeySFMCWithContactKey:(NSString * _Nonnull)contactKey SWIFT_WARN_UNUSED_RESULT;
- (void)setSFMCAttributeWithAttribute:(NSString * _Nonnull)attribute;
- (void)registerforSFMCtokenWithDeviceToken:(NSData * _Nonnull)deviceToken;
- (NSString * _Nonnull)getContactKeySFMC SWIFT_WARN_UNUSED_RESULT;
- (void)pushNotificationReceivedWithUserInfo:(NSDictionary * _Nonnull)userInfo fetchCompletionHandler:(void (^ _Nonnull)(UIBackgroundFetchResult))completionHandler;
- (void)sendPushAnalyticsWithRequst:(UNNotificationRequest * _Nonnull)requst;
- (void)setSFMCTagsWithTag:(NSString * _Nonnull)tag;
- (BOOL)configureMarketingCloudSDK SWIFT_WARN_UNUSED_RESULT;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

#if __has_attribute(external_source_symbol)
# pragma clang attribute pop
#endif
#pragma clang diagnostic pop

我猜这个头文件完全是 Xcode 生成的,但抛出异常 Attempting to use the forward class 'NSManagedObjectModel' as superclass of 'EventModel'

我已经多次清除Build文件夹并重新构建它,没有工作!。一些类似的帖子建议我可能有一个循环导入,但我无法找出问题所在,我只添加相关文件及其导入和项目层次结构的图像

我的核心数据模型命名

Unity-iPhone

代码生成设置为类别/扩展

EventModel+CoreDataClass.swift

import CoreData
public class EventModel: NSManagedObject 

iosEventTracker.mm

//
//  IOSEventTracker.m
//  Unity-iPhone
//
//  Created by ajey raj on 13/08/20.
//
#include "SwiftPlugin-Swift.h"

@interface IOSEventTracker : NSObject

@end

@implementation IOSEventTracker

class definition...

TNLSalesForce-Bridging-Header.h

//
//  TNLSalesForce-Bridging-Header.h
//  TNLSalesForce
//
//  Created by Ansar K.A. on 07/05/20.
//  Copyright © 2020 Ansar K.A. All rights reserved.
//


#ifndef TNLSalesForce_Bridging_Header_h
#define TNLSalesForce_Bridging_Header_h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "OverrideAppDelegate.h"
#import <CoreData/CoreData.h>

#endif /* TNLSalesForce_Bridging_Header_h */

OverrideAppDelegate.h

//
//  OverrideAppDelegate.h
//  Unity-iPhone
//
//  Created by Ansar K.A. on 09/06/20.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>
#import <CoreData/CoreData.h>

@interface OverrideAppDelegate : NSObject 
 declaration methods..
@end

OverrideAppDelegate.mm

//
//  OverrideAppDelegate.m
//  Unity-iPhone
//
//  Created by Ansar K.A. on 09/06/20.
//

#import <Foundation/Foundation.h>
#import "OverrideAppDelegate.h"
#include "SwiftPlugin-Swift.h"
#import <Firebase/Firebase.h>

@implementation OverrideAppDelegate
   class methods...
@end

Override_iOS.h

//
// Copyright (c) 2017 eppz! mobile, Gergely Borbás (SP)
//
// http://www.twitter.com/_eppz
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "EPPZSwizzler.h"
#import "OverrideAppDelegate.h"


@interface Override_iOS : NSObject

@end

Override_iOS.mm

#import "Override_iOS.h"
__strong Override_iOS *_instance;
@implementation Override_iOS

....
...
@end

项目层次结构

注意,这将是由统一生成的目标 c 项目,所有核心数据模型将通过后处理脚本推送到 Xcode。

【问题讨论】:

【参考方案1】:

您不能将转发类用作基类(超类)。

当你有一个前向类时,你和编译器只知道有一个同名的类。这些信息不足以创建子类。编译器需要类的实际接口。

【讨论】:

我相信你的前向类是指不能用作超类的“NSManagedObject”。但是头文件 'SwiftPlugin-Swift.h' 是由 Xcode 创建和管理的,无法编辑,如果我错了或无法理解你的答案,请见谅。请解释

以上是关于尝试使用前向类“NSManagedObjectModel”作为 Swift 类模型的超类的主要内容,如果未能解决你的问题,请参考以下文章

英特尔编译器不编译另一个类中的私有前向类声明

在前向类对象“UIImage”中找不到属性“CGImage”

在前向类对象“CALayer”中找不到属性“cornerRadius”

向类 org.springframework.web.context.ContextLoaderListener 的侦听器实例发送上下文初始化事件的异常

向类 org.apache.tomcat.websocket.server.WsContextListener 的侦听器实例发送上下文初始化事件的异常

使用springboot向类路径错误添加实现,例如Hibernate Validator