Page 1 of 1

【iOS】iOS18+Xcode16适配-代码规范

Posted: 2024年 Nov 8日 10:30
by Aaron

Xcode15打包应用在iOS18系统可以正常运行,而用Xcode16打包应用部分页面在iOS18系统上出现崩溃或编译报错,而崩溃的大部分原因是UI触发断言,经典错误如下:
1.
crash信息
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Expected dequeued view to be returned to the collection view in preparation for display. When the collection view's data source is asked to provide a view for a given index path, ensure that a single view is dequeued and returned to the collection view. Avoid dequeuing views without a request from the collection view. For retrieving an existing view in the collection view, use -[UICollectionView cellForItemAtIndexPath:] or -[UICollectionView supplementaryViewForElementKind:atIndexPath:]. Dequeued view: <xxxCell: 0x136cf0c00; baseClass = UICollectionViewCell; frame = (0 0; 434 726); backgroundColor = UIExtendedGrayColorSpace 0 1; layer = <CALayer: 0xxxx>>; Collection view: <xxxCollectionView: 0xxxx; baseClass = UICollectionView; frame = (-10 92; 434 726); clipsToBounds = YES; gestureRecognizers = <NSArray: 0xxxx>; backgroundColor = UIExtendedGrayColorSpace 0 1; layer = <CALayer: 0xxxx>; contentOffset: {0, 0}; contentSize: {434, 726}; adjustedContentInset: {0, 0, 0, 0}; layout: <UICollectionViewFlowLayout: 0xxxx>; dataSource: <xxxController: 0xxxx>>'
修改建议
复用类视图通过唯一标识获取到之后,在没有return之前不要再次通过同一标识再次获取复用视图,否则会触发断言

2.
crash信息
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Set 'maskView' (<UIView:0xxxx; frame =10 0;390,844); backgroundColor = ‹UIDynamicProviderColor: 0xxxx; provider = <NSMallocBlock: 0xxxx>>; layer = ‹CALayer:0xxxx>>) to 'nil' before adding it as a subview of<xxxPage:0xxxx; frame = (0 0; 0 0); layer = ‹CALayer:0xxxx>>
修改建议
把自定义UIView的子类中的maskView属性重命名

3.
crash信息
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'View was already initialized:
<View: 0xxxx; frame = (0 0; 0 0); layer = <CALayer:0xxxx>>
修改建议
修改代码逻辑,避免视图被连续初始化多次

4.
编译错误
方法名中使用module作为参数,而module为关键字导致编译报错

总结
1.上述crash可以看出,Apple对代码质量提高了要求,开发者要注重自身代码质量,否则可能会出现功能异常
2.类名和方法名禁止使用系统关键字
2.App使用Xcode16打包上架前,必须全面回归测试,否则可能会出现crash或功能异常