Patrick Patrick - 6 months ago 95
Objective-C Question

Convert or compare "Unmanaged<NSString> and NSString

I have a framework in Objective-C with this struct:

VehicleClass.h

struct VehicleStruct
{
__unsafe_unretained NSString *const CAR;
__unsafe_unretained NSString *const MOTORCYCLE;
__unsafe_unretained NSString *const TRUCK;
};


VehicleClass.m

#import "VehicleClass.h"

const struct VehicleStruct Vehicle = {
.CAR = @"CAR",
.MOTORCYCLE = @"MOTORCYCLE",
.TRUCK = @"TRUCK"
};


And in my Contansts.h

extern const struct VehicleStruct Vehicle;


Well, now I want get car value in my proyect using swift:

let carValueSwift = String(Vehicle.CAR)


In console:


"Unmanaged NSString(_value: CAR)"


How can I solve this? I need get this value like NSString, because if then I want to compare in my framework:

["CAR" isEqual: carValueSwift]
Doesn´t work.

Thank you very much

Answer

With takeUnretainedValue() the Unmanaged<NSString> is converted to NSString (without taking ownership), and this can simply be cast to a Swift String:

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