Patrick Patrick - 1 year ago 197
Objective-C Question

Convert or compare "Unmanaged<NSString> and NSString

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


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


#import "VehicleClass.h"

const struct VehicleStruct Vehicle = {
.CAR = @"CAR",

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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download