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