项目与oc
和swift
混合在一起。
#import "myProjectName-Swift.h"
AppDelegate.m
,:
项目是我创造Objective-c
语言,几乎用swift
语言来写。 在Appdelegate.m
我想使用swift
的类,等等
但不幸的是,我的项目发生在myProjectName-Swift.h
error
:
代码如下:
SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly, copy)
NSString * _Nonnull ;)
+ (NSString * _Nonnull);
SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly, copy)
NSString * _Nonnull ;)
+ (NSString * _Nonnull);
SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly, strong) UIColor * _Nonnull APP_COLOR;)
+ (UIColor * _Nonnull)APP_COLOR;
错误是:
Expected ';' at end of declaration list.
cannot declare variable inside @interface or @protocal
Expected member name or ';' after declaration specifiers
Expected ']'
Property requires fields to be named
Missing '[' at start of message send expression
等等...
Attension
在全局swift
中,我像这样设置汉字变量:
class Global: NSObject {
}
>编辑 - 1因为我认为错误指出static let "
的,so I annotation this line, project will not show this
错误. Or I delete the
,'也不会显示错误。
Objective-C 不支持标识符(变量名、类名、方法名等)中的 Unicode,仅在字符串文字 (" ") 中支持。只允许 ASCII 的一个子集(以 _[a-zA-Z] 开头,后面以 +[0-9] 开头)。
例:
class MyClass : NSObject {
func () { }
}
当您在混合项目中编译它时,这将中断。相反,你需要给它一个有效的Objective-C名称,如下所示:
class MyClass : NSObject {
@objc(unhappyFace)
func () { }
}
将编译(您需要在 ObjC 端unhappyFace
访问它)。
或者在您的示例中,您需要修改 Global 对象:
class Global: NSObject {
@objc(emptyNumberNotAllowed)
static let 手机号码不能为空 = "手机号码不能为空"
... etc ...
}