As in my last blog post, this problem is also in the custom tabbar. In my last blog post, I determined whether a certain type has taken UIImageView and UILabel that are placed on UIView. In order to change the latter, I must change the type. And then the blogger went there and tried it out with what he knew, and it turned out, look at the code:
Original object-c code:
UIImageView * imageView=(UIImageView *)[view.subviews objectAtIndex:0];
UILabel * label=(UILabel *)[view.subviews objectAtIndex:1];
Copy the code
Swift Swift Swift Swift Swift Swift Swift Swift Swift
let imageView = view.subviews[0] as! UIImageView
let label = view.subviews[1] as! UILabel
Copy the code
The following for as? And the as! To explain:
First of all, they're used to convert types and second of all, as? Provides a fault-tolerant mechanism that converts to the corresponding type if it succeeds, otherwise nil, as! '! In the case of system crash, errors may occur during operation. However, we generally use it to determine the type. If the crash, remember to check the type. Also, Swift is very strict about type matching and must be used with caution.Copy the code