In my case, the compiler wanted me to write it in such a way as to suppress all warnings and compilation problems, so that not only this exclamation mark, even if the imagesField field is already declared with one, but also brackets and "how!" to make sure no one complains.
(imagesField!.images as! [UIImage]) ๐คฎ
It made me feel rather awkward ... Swift might be better, his new language is so ... I made an extension:
public static func cast(_ object: Any) -> Self { return object as! Self }
This is assigned to the array:
extension Array: CSLang { }
And now I can write the same statement as this with the same effect:
[UIImage].cast(imagesField.images)
Like it or not, this is my way, fewer questions and exclamation points are better. I also did a unit test:
func testCast() { let array: NSMutableArray? = NSMutableArray() array?.add("test string 1") array?.add("test string 2") let stringArray: [String] = [String].cast(array) XCTAssertEqual("test string 2", stringArray[1]) }
Renetik Jan 21 '19 at 18:21 2019-01-21 18:21
source share