Prevent takeRetainedValue or takeUnretainedValue when using an Objective-C Struct

ساخت وبلاگ

Vote count: 0

I'm using this approach to keep my string constants together. Using the same example from that post:

MONExtResult.h

struct MONExtResultStruct { __unsafe_unretained NSString * const AppID; __unsafe_unretained NSString * const ErrorCode; __unsafe_unretained NSString * const Progress;
};
exte const struct MONExtResultStruct MONExtResult;

MONExtResult.m

const struct MONExtResultStruct MONExtResult = { .AppID = @"appid", .ErrorCode = @"errorcode", .Progress = @"progress"
};

When I try to use it in Swift:

let appID: String = MONExtResult.AppID

I get the error:

Cannot convert value of type 'Unmanaged<NSString>!' to expected argument type 'String'

This is because I need to grab the value from the unmanaged wrapper and then convert it to a String:

let appID: String = MONExtResult.AppID.takeUnretainedValue() as String

Is there any way to annotate the objective-C code to prevent the need for calling takeUnretainedValue like you can with CF_IMPLICIT_BRIDGING_ENABLED or CF_RETURNS_RETAINED for C functions?

asked 55 secs ago

back soft...
ما را در سایت back soft دنبال می کنید

برچسب : نویسنده : استخدام کار backsoft بازدید : 250 تاريخ : يکشنبه 6 تير 1395 ساعت: 22:46