objc文件中有一个宏,如下所示。
#define kSOME_COLOR [UIColor colorWithRed:100/255.0 green:100/255.0 blue:100/255.0 alpha:1.0]
我想从Swift访问宏。
self.someView.backgroundColor = kSOME_COLOR
但我收到编译错误Use of unresolved identifier 'kNOTE_GREEN_COLOR'
有什么办法。
答案 0 :(得分:1)
Leo是对的,你不能像Swift中的Macro那样使用Objective C.你可以在快速课堂上重新定义它们。
或者,如果您只想从目标c类中使用它们,那么您可以执行以下操作 在.m文件实现中,并在标题
中公开exports.handler = function(event, context) {
// Get the object from the event and show its content type
var bucket = event.Records[0].s3.bucket.name;
var key = event.Records[0].s3.object.key;
var params = {
Bucket: bucket,
Key: key
};
s3.getObject(params, function(err, data) {
if (err) {
...
} else {
processFile(data.Body.toString(), 0);
console.log("ok");
}
});
//need to wait here till processFile is done
};
processFile = function(content, start) {
... build url to call
http.get(url, function(res) {
console.log("Got response: " + res.statusCode + ");
processFile(content, start + 1);
});
}
<。>文件中的
+ (UIColor *)k_SOME_COLOR {
return kSOME_COLOR;
}
在swift文件中
+ (UIColor *)k_SOME_COLOR;
为此你需要使用Swift Bridging标头并在桥接头中包含你的常量类,调用Objective-C代码的其余转换只是Swift的魔力。 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
答案 1 :(得分:0)
Swift中没有宏,但你可以得到完全相同的结果,定义一个常量如下:
let someGrayColor = UIColor(red: 100.0/255.0, green: 100.0/255.0, blue: 100.0/255.0, alpha: 1.0)
self.someView.backgroundColor = someGrayColor