Xcode16 避坑

Xcode16 避坑

Xcode16 避坑

erick_yim

2024-06-17 4,685 阅读3分钟

iPad 升级到iOS 18 后,底部的 UITabBar 会被系统移动到顶部。(如果你对页面 UI 尺寸做了调整,页面底部可能有空白)

如何把 UITabBar 移到底部?

iPadOS 18.1 + Xcode 16.1 仅仅设置self.mode = UITabBarControllerModeTabBar不起作用

复制代码
// 在UITabBarController子类里

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 180000
    if (@available(iOS 18.0, *)) {
        if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
            self.mode = UITabBarControllerModeTabBar;
            self.traitOverrides.horizontalSizeClass = UIUserInterfaceSizeClassCompact;
            [self.view addSubview:self.tabBar];
            NSString *tabContainerClassName = [NSString stringWithFormat:@"%@%@%@", @"_UITab", @"Container", @"View"];
            for (UIView *subview in self.view.subviews) {
                if ([NSStringFromClass(subview.class) isEqualToString:tabContainerClassName]) {
                    [subview setHidden:YES];
                }
            }
        }
    }
#endif

2、第一次断点po慢,输出 Debugging will be degraded due to missing types.

unable to locate module needed for external types

error:

'/xxxxx/ModuleCache.noindex/C499AKLJ8KAG/ObjectiveC-1KD62J152BYGO.pcm' does

not exist

Debugging will be degraded due to missing types. Rebuilding the project will

regenerate the needed module files.warning: (arm64)

Debugging will be degraded due to missing types. Rebuilding the project will

regenerate the needed module files.warning: (arm64)

  1. 创建或者编辑文件echo "settings set target.experimental.swift-enable-cxx-interop false" >> ~/.lldbinit
    2 重启进程killall lldb-rpc-server
  2. 打开Xcode重新debug

似乎在 Build Settings - Build Options里设置 DEBUG_INFORMATION_FORMAT = "dwarf- with-dsym" 有效

XCode15 debugger is working really slow with iOS 17.0.2
forums.developer.apple.com/forums/thre...

3、自定义组件的 maskView 崩溃

Terminating app due to uncaught exception 'NSInternalInconsistencyException',

reason: 'Set maskView (<UIView: 0x1155af800; ...) to nil before adding it as

a subview of xxx

原因: UIView有自带属性 maskView, iOS 18 对 UIView的maskView

增加了断言,导致如果业务代码里有同名属性可能导致触发该断言。自定义组件相当于重写了系统属性。

解决方法:将自定义组件的 maskView 重名名。

复制代码
// UIView.h
@interface UIView(UIViewRendering)
@property(nullable, nonatomic,strong) UIView *maskView API_AVAILABLE(ios(8.0));
#end

4、git仓库有大量的staged文件, 导致Xcode Not Responding

当前版本:Xcode Version 16.1 (16B40), 已经向苹果反馈。

出现问题的场景:

1)当前项目初始化了git仓库,有大量的staged文件但是没有提交(比如5000个)。

Xcode重新打开项目时也无响应。其实Xcode在处理文件导致不能操作, 可能10分钟后才处理完毕。

解决方法:git commit 即可

5、Xcode 编辑支持的设备方向时崩溃

![image.png](https://p3-xtjj-sign.byteimg.com/tos-

cn-i-73owjymdk6/ad92e8e238ea4c648aca9d5570169814~tplv-73owjymdk6-jj-

mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgZXJpY2tfeWlt:q75.awebp?rk3s=f64ab15b&x-expires=1760437608&x-signature=J3cWgTPYvu7sD1%2BfIZ%2B1XD36VPQ%3D)

旧版本创建的Xcode项目,支持的设备方向的字段在info.plist文件中,Xcode 16

移除了info.plist中的字段,改为project中配置所以尝试编辑时崩溃。

解决方法:

1). 找到info.plist文件,先备份 2). 右键点击,选择open as source code,删除所有key value,保留如下

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
</dict>
</plist>

3). 重新打开项目,可以看到支持的设备方向已经清空了,现在可以重新选择支持的方向

![image.png](https://p3-xtjj-sign.byteimg.com/tos-

cn-i-73owjymdk6/55e9e18aca28410d9178fe756e24cc50~tplv-73owjymdk6-jj-

mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgZXJpY2tfeWlt:q75.awebp?rk3s=f64ab15b&x-expires=1760437608&x-signature=EYy1bSY8pGUCG%2BY7p%2Ff2y12r%2BwI%3D)

4). 把需要的key value 重新添加到info.plist里面

这些在项目配置中有的key就不要添加到info.plist,否则修改支持的设备方向还是会崩溃

![image.png](https://p3-xtjj-sign.byteimg.com/tos-

cn-i-73owjymdk6/f81af769f6dd408684d146911a4d22ef~tplv-73owjymdk6-jj-

mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgZXJpY2tfeWlt:q75.awebp?rk3s=f64ab15b&x-expires=1760437608&x-signature=ZlWmlgpJfcslgwthLNBR6BHH3oQ%3D)

6、openURL 废弃 (无法打开微信等第三方APP)

BUG IN CLIENT OF UIKIT: The caller of UIApplication.openURL(_:) needs to

migrate to the non-deprecated
UIApplication.open(_:options:completionHandler:). Force returning false

(NO).

UIApplication.shared.open(URL) 替换成 UIApplication.shared.open(URL, options: [:], completionHandler: nil)

7、iOS 18 PHAsset URL from requestAVAsset

获取相册文件PHImageManager.default().requestAVAsset(forVideo...)

Xcode 16/15 + iOS 18 返回的absoluteString包含一段奇怪的固定代码
#YnBsaXN0MDDRAQJfEBtSZWNvbW1lbmRlZEZvckltbWVyc2l2ZU1vZGUQAAgLKQAAAAAAAAEBAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAr

复制代码
PHCachingImageManager().requestAVAsset(  
forVideo: userPhoto.imageAsset,  
options: options  
) { asset, _, _ in  
if let asset = asset as? AVURLAsset {  
print(asset.url.absoluteString)  
// file:///var/mobile/Media/DCIM/100APPLE/IMG_0768.MP4#YnBsaXN0MDDRAQJfEBtSZWNvbW1lbmRlZEZvckltbWVyc2l2ZU1vZGUQAAgLKQAAAAAAAAEBAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAr 
  
print(asset.url.relativePath)  
// /var/mobile/Media/DCIM/100APPLE/IMG_0768.MP4
}  
}

检查文件是否存在

复制代码
let relativePath = someUrl.relativePath
let absolutePath = someUrl.absoluteString



FileManager.default.fileExists(atPath: relativePath) // true
FileManager.default.fileExists(atPath: absolutePath) // false

解决方法: 用relativePath替换absoluteString

8、UICollectionView两次dequeueReusableCell闪退

Xcode 16.2 + iOS 18.3.2, 以下代码会崩溃(神不神奇,意不意外?)

复制代码
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Identifier1", for: indexPath)
if condition {
    let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "Identifier2", for: indexPath)
    return cell2
}
return cell

有崩溃堆栈, 但是xcode无法定位到具体的崩溃地址

![image.png](https://p3-xtjj-sign.byteimg.com/tos-

cn-i-73owjymdk6/400501612cac47c1adb2002c33816a57~tplv-73owjymdk6-jj-

mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgZXJpY2tfeWlt:q75.awebp?rk3s=f64ab15b&x-expires=1760437608&x-signature=BaGmNGLmShIhagUVxrdP0A7DjyY%3D)

解决方法:

复制代码
if condition {
    let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "Identifier2", for: indexPath)
    return cell2
} else {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Identifier1", for: indexPath)
    return cell
}

9、Xcode 16.3 Failed to find or create execution context

Xcode 16.3 打开旧项目编译报错:

xxx.xib: Failed to find or create execution context for description

'<IBCocoaTouchPlatformToolDescription: 0x6000037d36a0> System content for

IBCocoaTouchFramework-ThirteenAndLater <IBScaleFactorDeviceTypeDescription:

0x6000037d3d20> scaleFactor=2x, renderMode.identifier=(null)'.

![image.png](https://p3-xtjj-sign.byteimg.com/tos-

cn-i-73owjymdk6/56002da19bb849fa979cd759d75a78b5~tplv-73owjymdk6-jj-

mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgZXJpY2tfeWlt:q75.awebp?rk3s=f64ab15b&x-expires=1760437608&x-signature=g6pwBWugT%2B1waIc5686tl3%2B2dco%3D)

原因未知

解决方法 :

复制代码
sudo killall -9 com.apple.CoreSimulator.CoreSimulatorService

xcrun simctl erase all


        for (UIView *subview in self.view.subviews) {
            if ([NSStringFromClass(subview.class) isEqualToString:tabContainerClassName]) {
                [subview setHidden:YES];
            }
        }
    }
}

#endif

2、第一次断点po慢,输出 Debugging will be degraded due to missing types.

unable to locate module needed for external types

error: '/xxxxx/ModuleCache.noindex/C499AKLJ8KAG/ObjectiveC-1KD62J152BYGO.pcm' does not exist

Debugging will be degraded due to missing types. Rebuilding the project will regenerate the needed module files.warning: (arm64)

Debugging will be degraded due to missing types. Rebuilding the project will regenerate the needed module files.warning: (arm64)

  1. 创建或者编辑文件 echo "settings set target.experimental.swift-enable-cxx-interop false" >> ~/.lldbinit
    2 重启进程 killall lldb-rpc-server
  2. 打开Xcode重新debug
    似乎在 Build Settings - Build Options里设置 DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym" 有效

XCode15 debugger is working really slow with iOS 17.0.2

forums.developer.apple.com/forums/thre...

3、自定义组件的 maskView 崩溃

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Set maskView (<UIView: 0x1155af800; ...) to nil before adding it as a subview of xxx

原因: UIView有自带属性 maskView, iOS 18 对 UIView的maskView 增加了断言,导致如果业务代码里有同名属性可能导致触发该断言。自定义组件相当于重写了系统属性。

解决方法:将自定义组件的 maskView 重名名。

objc 体验AI代码助手 代码解读复制代码// UIView.h

@interface UIView(UIViewRendering)

@property(nullable, nonatomic,strong) UIView *maskView API_AVAILABLE(ios(8.0));

#end

4、git仓库有大量的staged文件, 导致Xcode Not Responding

当前版本:Xcode Version 16.1 (16B40), 已经向苹果反馈。

出现问题的场景:

1)当前项目初始化了git仓库,有大量的staged文件但是没有提交(比如5000个)。

Xcode重新打开项目时也无响应。其实Xcode在处理文件导致不能操作, 可能10分钟后才处理完毕。

解决方法:git commit 即可

5、Xcode 编辑支持的设备方向时崩溃

旧版本创建的Xcode项目,支持的设备方向的字段在info.plist文件中,Xcode 16 移除了info.plist中的字段,改为project中配置所以尝试编辑时崩溃。

解决方法:

1). 找到info.plist文件,先备份

2). 右键点击,选择open as source code,删除所有key value,保留如下

xml 体验AI代码助手 代码解读复制代码<?xml version="1.0" encoding="UTF-8"?>

3). 重新打开项目,可以看到支持的设备方向已经清空了,现在可以重新选择支持的方向

4). 把需要的key value 重新添加到info.plist里面

这些在项目配置中有的key就不要添加到info.plist,否则修改支持的设备方向还是会崩溃

6、openURL 废弃 (无法打开微信等第三方APP)

BUG IN CLIENT OF UIKIT: The caller of UIApplication.openURL(😃 needs to migrate to the non-deprecated UIApplication.open(:options:completionHandler:). Force returning false (NO).

UIApplication.shared.open(URL)

替换成

UIApplication.shared.open(URL, options: [:], completionHandler: nil)

7、iOS 18 PHAsset URL from requestAVAsset

获取相册文件PHImageManager.default().requestAVAsset(forVideo...)

Xcode 16/15 + iOS 18 返回的absoluteString包含一段奇怪的固定代码

#YnBsaXN0MDDRAQJfEBtSZWNvbW1lbmRlZEZvckltbWVyc2l2ZU1vZGUQAAgLKQAAAAAAAAEBAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAr

swift 体验AI代码助手 代码解读复制代码PHCachingImageManager().requestAVAsset(

forVideo: userPhoto.imageAsset,

options: options

) { asset, _, _ in

if let asset = asset as? AVURLAsset {

print(asset.url.absoluteString)

// file:///var/mobile/Media/DCIM/100APPLE/IMG_0768.MP4#YnBsaXN0MDDRAQJfEBtSZWNvbW1lbmRlZEZvckltbWVyc2l2ZU1vZGUQAAgLKQAAAAAAAAEBAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAr

print(asset.url.relativePath)

// /var/mobile/Media/DCIM/100APPLE/IMG_0768.MP4

}

}

检查文件是否存在

swift 体验AI代码助手 代码解读复制代码let relativePath = someUrl.relativePath

let absolutePath = someUrl.absoluteString

swift 体验AI代码助手 代码解读复制代码FileManager.default.fileExists(atPath: relativePath) // true

FileManager.default.fileExists(atPath: absolutePath) // false

解决方法: 用relativePath替换absoluteString

8、UICollectionView两次dequeueReusableCell闪退

Xcode 16.2 + iOS 18.3.2, 以下代码会崩溃(神不神奇,意不意外?)

swift 体验AI代码助手 代码解读复制代码let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Identifier1", for: indexPath)

if condition {

let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "Identifier2", for: indexPath)

return cell2

}

return cell

有崩溃堆栈, 但是xcode无法定位到具体的崩溃地址

解决方法:

swift 体验AI代码助手 代码解读复制代码

if condition {

let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "Identifier2", for: indexPath)

return cell2

} else {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Identifier1", for: indexPath)

return cell

}

9、Xcode 16.3 Failed to find or create execution context

Xcode 16.3 打开旧项目编译报错:

xxx.xib: Failed to find or create execution context for description '<IBCocoaTouchPlatformToolDescription: 0x6000037d36a0> System content for IBCocoaTouchFramework-ThirteenAndLater <IBScaleFactorDeviceTypeDescription: 0x6000037d3d20> scaleFactor=2x, renderMode.identifier=(null)'.

原因未知

解决方法:

zsh 体验AI代码助手 代码解读复制代码sudo killall -9 com.apple.CoreSimulator.CoreSimulatorService

xcrun simctl erase all

作者:erick_yim

链接:https://juejin.cn/post/7381347654767689737

来源:稀土掘金

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关推荐
tangweiguo030519874 小时前
SwiftUI布局完全指南:从入门到精通
ios·swift
T1an-18 小时前
最右IOS岗一面
ios
坏小虎10 小时前
Expo 快速创建 Android/iOS 应用开发指南
android·ios·rn·expo
光影少年11 小时前
Android和iOS原生开发的基础知识对RN开发的重要性,RN打包发布时原生端需要做哪些配置?
android·前端·react native·react.js·ios
北京自在科技11 小时前
Find My 修复定位 BUG,AirTag 安全再升级
ios·findmy·airtag
Digitally12 小时前
如何不用 USB 线将 iPhone 照片传到电脑?
ios·电脑·iphone
Sim14801 天前
iPhone将内置本地大模型,手机端AI实现0 token成本时代来临?
人工智能·ios·智能手机·iphone
Digitally1 天前
如何将 iPad 上的照片传输到 U 盘(4 种解决方案)
ios·ipad
报错小能手1 天前
ios开发方向——swift并发进阶核心 @MainActor 与 DispatchQueue.main 解析
开发语言·ios·swift
LcGero1 天前
Cocos Creator 业务与原生通信详解
android·ios·cocos creator·游戏开发·jsb