2021年2月6日星期六

How to extend an optional generic Array?

I'm trying to add an extension on optional arrays so I can do this

var test: [String]?    XCTAssert(test.isNilOrEmpty)    test = []  XCTAssert(test.isNilOrEmpty)    test = ["abc"]  XCTAssertFalse(test.isNilOrEmpty)  

I cannot get the extension syntax correct, this is what I'm trying:

extension Optional where Wrapped: Array { // Error: Reference to generic type 'Array' requires arguments in <...>        var isNilOrEmpty: Bool { self?.isEmpty ?? true }  }  

I tried using Array<Any> but didn't let me apply to any type. What's the right syntax do achieve this?

https://stackoverflow.com/questions/66083890/how-to-extend-an-optional-generic-array February 07, 2021 at 10:02AM

没有评论:

发表评论