【发布时间】:2016-10-28 15:35:03
【问题描述】:
我使用this approach 将我的字符串常量放在一起。使用该帖子中的相同示例:
MONExtResult.h
struct MONExtResultStruct {
__unsafe_unretained NSString * const AppID;
__unsafe_unretained NSString * const ErrorCode;
__unsafe_unretained NSString * const Progress;
};
extern const struct MONExtResultStruct MONExtResult;
MONExtResult.m
const struct MONExtResultStruct MONExtResult = {
.AppID = @"appid",
.ErrorCode = @"errorcode",
.Progress = @"progress"
};
这可以像这样在 Objective-C 中使用:
NSString *str = MONExtResult.AppID;
当我尝试在 Swift 中使用它时:
let appID: String = MONExtResult.AppID
...我收到错误:
无法转换“Unmanaged
!”类型的值到预期的参数类型“字符串”
这是因为我需要从unmanaged wrapper 中获取值,然后将其转换为字符串:
let appID: String = MONExtResult.AppID.takeUnretainedValue() as String
是否有任何方法可以注释 Objective-C 代码以防止需要调用 takeUnretainedValue,就像使用 CF_IMPLICIT_BRIDGING_ENABLED 或 CF_RETURNS_RETAINED 调用 C 函数一样?
更新: 常量必须可以被 Objective-C 和 Swift 访问。否则我只会使用带有 String 原始值的 Swift 枚举。
【问题讨论】:
标签: objective-c c swift memory-management automatic-ref-counting