@@ -199,6 +199,11 @@ testCases = [
199
199
]
200
200
}%
201
201
202
+ /// Whether this can be safely casted to NSObject
203
+ func isNSObject<T>(_ value: T) -> Bool {
204
+ return (value as? NSObject) != nil
205
+ }
206
+
202
207
% for testName, type, valueExpr, testFunc, conformsToError, conformsToHashable in testCases:
203
208
BridgeAnything.test("${testName}") {
204
209
autoreleasepool {
@@ -210,7 +215,7 @@ BridgeAnything.test("${testName}") {
210
215
let xInArray = [x]
211
216
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInArray) as! [AnyObject])[0])
212
217
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInArray) as? [AnyObject])![0])
213
- if (x as? NSObject) != nil {
218
+ if isNSObject(x) {
214
219
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInArray) as! [AnyObject])[0])
215
220
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInArray) as? [AnyObject])![0])
216
221
}
@@ -219,7 +224,7 @@ BridgeAnything.test("${testName}") {
219
224
let xInDictValue = ["key" : x]
220
225
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictValue) as! [String: AnyObject])["key"]!)
221
226
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictValue) as? [String: AnyObject])!["key"]!)
222
- if (x as? NSObject) != nil {
227
+ if isNSObject(x) {
223
228
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictValue) as! [String: NSObject])["key"]!)
224
229
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictValue) as? [String: NSObject])!["key"]!)
225
230
}
@@ -231,7 +236,7 @@ BridgeAnything.test("${testName}") {
231
236
// The NSObject version below can't test class LifetimeTracked.
232
237
// ${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictKey) as! [(AnyObject & Hashable): String]).keys.first!)
233
238
// ${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictKey) as? [(AnyObject & Hashable): String])!.keys.first!)
234
- if (x as? NSObject) != nil {
239
+ if isNSObject(x) {
235
240
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictKey) as! [NSObject: String]).keys.first!)
236
241
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictKey) as? [NSObject: String])!.keys.first!)
237
242
}
0 commit comments