Objective c 转换或比较;非托管<;NSString>;和NSString

Objective c 转换或比较;非托管<;NSString>;和NSString,objective-c,swift,struct,Objective C,Swift,Struct,我有一个Objective-C框架,结构如下: 车辆等级h struct VehicleStruct { __unsafe_unretained NSString *const CAR; __unsafe_unretained NSString *const MOTORCYCLE; __unsafe_unretained NSString *const TRUCK; }; extern const struct VehicleStruct Vehicle; 车辆等级.

我有一个Objective-C框架,结构如下:

车辆等级h

struct VehicleStruct
{
    __unsafe_unretained NSString *const CAR;
    __unsafe_unretained NSString *const MOTORCYCLE;
    __unsafe_unretained NSString *const TRUCK;
};
extern const struct VehicleStruct Vehicle;
车辆等级.m

#import "VehicleClass.h"

const struct VehicleStruct Vehicle = {
    .CAR = @"CAR",
    .MOTORCYCLE = @"MOTORCYCLE",
    .TRUCK = @"TRUCK"
};
在我的Contants.h

struct VehicleStruct
{
    __unsafe_unretained NSString *const CAR;
    __unsafe_unretained NSString *const MOTORCYCLE;
    __unsafe_unretained NSString *const TRUCK;
};
extern const struct VehicleStruct Vehicle;
嗯,现在我想在我的项目中使用swift获取汽车价值:

let carValueSwift = String(Vehicle.CAR)
控制台中

let carValueSwift = String(Vehicle.CAR)
“非托管NSString(_值:CAR)”

我怎样才能解决这个问题?我需要像NSString一样获取此值,因为如果要在我的框架中进行比较:

[“CAR”isEqual:carValueSwift]
不起作用


非常感谢

嗯,我找到了解决方案:

let car: Unmanaged<NSString> = Vehicle.CAR

let carString = convertCfTypeToString(car)
func convertCfTypeToString(cfValue: Unmanaged<NSString>!) -> String?
{
   let value = Unmanaged.fromOpaque(
       cfValue.toOpaque()).takeUnretainedValue() as CFStringRef
   if CFGetTypeID(value) == CFStringGetTypeID(){
       return value as String
   } else {
       return nil
   }
}
let car:Unmanaged=Vehicle.car
让carString=convertCfTypeToString(car)
功能:

let car: Unmanaged<NSString> = Vehicle.CAR

let carString = convertCfTypeToString(car)
func convertCfTypeToString(cfValue: Unmanaged<NSString>!) -> String?
{
   let value = Unmanaged.fromOpaque(
       cfValue.toOpaque()).takeUnretainedValue() as CFStringRef
   if CFGetTypeID(value) == CFStringGetTypeID(){
       return value as String
   } else {
       return nil
   }
}
func convertCfTypeToString(cfValue:非托管!)->字符串?
{
let value=Unmanaged.from不透明(
cfValue.toOpaque())。将未恢复的值()作为CFStringRef
如果CFGetTypeID(值)=CFStringGetTypeID(){
以字符串形式返回值
}否则{
归零
}
}

如果有人知道避免使用
convertCfTypeToString:
方法的其他方法,请告诉我……

使用
takeUnretainedValue()
将转换
非托管的
NSString
(不取得所有权),这可以简单地 转换为Swift
字符串

let carValueSwift = Vehicle.CAR.takeUnretainedValue() as String
print(carValueSwift)

回答得好;)感谢您使用
\uuuuuu unsafe\uu unrepaired
而不是
的任何原因?\uuuu弱给出下一个错误:ARC禁止在结构中使用Objective-C对象当然,您不能在结构中使用