- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
首先我知道这个:How do I get a reference to the app delegate in Swift?
其次,我需要做的是在混合应用程序的 Swift 端访问 appdelegate 属性。
基本上,
1- 我有一个作为 Objective C 项目启动的项目。这意味着 AppDelegate 是在 Objective C 端定义的。
2- 我的 swift 代码工作正常,我有一个桥头,我可以从另一边的任何一边引用东西。
3- 这是问题所在:要在我的 Swift 代码中引用 appdelegate,我需要在我的桥接 header 中添加 #import "AppDelegate.h"
。但出于其他原因,我还需要 AppDelegate.h 来导入 SWift header ( PROJECT-Swift.h
)。这会创建一个引用循环。
有没有办法避免这个引用循环?并仍然访问 AppDelegate 属性?
编辑:我在问题的第一版中没有提到的另一个复杂问题是,我想向 Swift 代码公开的 AppDelegate 属性实际上是在 Swift 端声明的类型。所以我需要在 AppDelegate.h
中声明它,为了能够做到这一点,我需要在我的 AppDelegate 中导入
.-Swift.h
header 。 h
为了更清楚:KB
是一个定义在Swift端的public class
。
AppDelegate 具有如下属性:@property (strong) KB *appkb;
我需要获取 ((AppDelegate*)UIApplication.SharedApplication()).appkb
最佳答案
您应该在 AppDelegate.m
中导入 PROJECT-Swift.h
,而不是 .h
在 AppDelegate.h
中,您可以使用“前向声明”(@class
和 @protocol
),例如:
AppDelegate.h:
#import <UIKit/UIKit.h>
@class SwiftClass;
@class KB;
@protocol SwiftProtocol;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) id<SwiftProtocol> swifter;
@property (strong, nonatomic) KB *appkb;
-(void)methodReceiveSwiftClass:(SwiftClass *)obj;
//...
@end
AppDelegate.m:
#import "AppDelegate.h"
#import "PROJECT-Swift.h"
@implemetation AppDelegate
//....
@end
PROJECT-Bridging-Header.h
#import "AppDelegate.h"
任何.swift:
@objc public protocol SwiftProtocol {
// ...
}
@objc public class SwiftClass:NSObject {
// ...
}
@objc public class KB:NSObject {
// ...
}
To avoid cyclical references, don’t import Swift into an Objective-C header file. Instead, you can forward declare a Swift class to use it in an Objective-C header. Note that you cannot subclass a Swift class in Objective-C.
关于objective-c - Ho 在混合应用程序(主要语言 ObjC)中从 Swift 获取对 appdelegate 的引用以避免引用循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26911042/
我正在尝试在@objc 协议(protocol)中创建一个可选函数,文档类型也是@objc 枚举。但是我收到了这个错误: Method cannot be marked @objc because t
我有一个像这样定义的 objective-c API: - (instancetype)initWithItems:(NSArray *)items reuseIdentifier:(NSString
我有一个 objc 程序,我想使用一个用 objc++ 编写的小部件(即 https://launchpad.net/scintilla-cocoa )。我该怎么做?基本上我想要一个新的窗口 Cont
默认情况下,当您请求一个新的 ObjC 类时,Xcode 会同时创建一个 .h 和一个 .m 文件。一切正常,直到您需要在项目的其他地方引用任何 C++ 文件并开始#import 将其导入 .h 或
我正在尝试在我的 objective-c 项目中添加 swift 库 https://github.com/vimeo/VimeoUpload 我已经在我的项目中添加了 #import “-Swift
我的项目名称是ObjCSwift。创建项目后,我使用 xib 创建一个新的 swift 文件 SecondViewController.swift。 我已在 AppDelegate.m 中导入了 #i
同时 here Apple 声明 Swift available 标志也应该应用于 objc,它对我不起作用。我做错了什么? 我在 Swift 文件中有以下声明: @objc protocol Rea
我创建了一个 echo 命令的克隆,这比我想象的要复杂。这是我的问题:char *argv[] 是如何工作的?我知道 char myString[] 是如何工作的,但没有那种创建字符串的奇怪方法: 是
有什么区别: @objc class MyClass: NSObject{} 和 @objc(MyClass) class MyClass: NSObject{} 最佳答案 @objc 修饰符在 Sw
考虑: class SomeCppClass { public: SomeCppClass() {} ; ~SomeCppClass() {} ; } ; @interface Tes
我目前正在 UIButton 类别中使用 block 。但是我遇到了这个无法解决的错误: error: /usr/include/objc/objc-class.h: No such file or
我正在 Objective C 中构建一个动态框架,我已经使用 Swift 单元测试目标进行了单元测试。到目前为止,这一直运行良好。 我现在需要将我正在构建的框架与另一个 obj-c 动态框架链接起来
我经常会开始一个小的“辅助类”,发现我可以用 C 函数完全满足我的需求(尽管这些函数可能经常在其中包含 ObjC,可能返回类型或 NSDictionary* 的一些参数或其他什么..),所以我将删除@
在 .mm 文件中混合使用 Objective C 和 C++ 时,是否存在任何陷阱,我是否应该尽可能坚持使用纯 ObjC? 最佳答案 除了 Objective-C 编程指南的 Objective-C
这个问题在这里已经有了答案: Non-'@objc' method does not satisfy optional requirement of '@objc' protocol (3 个答案)
我正在尝试使用条件扩展创建 MKMapViewDelegate 的默认实现,如下所示: extension MKMapViewDelegate where Self: NSObject {
概述: 我有一个协议(protocol) P1,它提供了一个 Objective-C 可选函数的默认实现。 当我提供可选函数的默认实现时,会出现警告 编译器警告: Non-'@objc' method
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我有一个情况。如果有人对此有解决方案,我将不胜感激 我有一个 objC enum 说 Abc 我在一个 swift 类中将其声明为 MySwiftClass.swift var abc : Abc!
我试图从 Objective-C 类调用此函数,但遇到了问题。我收到错误消息“方法无法标记为@objc,因为参数 3 的类型无法在 Objective-C 中表示” 我假设问题出在“错误”参数上,但不
我是一名优秀的程序员,十分优秀!