You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xcode升级至15后PhotosPicker设置maxSelectionCount>1时无法多选问题

Xcode 15 下 PhotosPicker 多选失效问题解决

以下代码在Xcode 14.3可正常运行,但升级到Xcode 15后,当maxSelectionCount设为大于1的值时,PhotosPicker打开相册选中照片后会自动取消选中,无法完成多选;仅当maxSelectionCount=1时功能正常。

PhotosPicker(selection: $vm.selectedItemsTest, 
             maxSelectionCount: 10, 
             selectionBehavior: .continuous, 
             matching: .images) {
                 Image(systemName: "trophy")
                     //.font(.system(size: scaled16*1.5))
                     .font(Font.system(size: scaled16*1.5, weight: .regular))
                     .foregroundColor(Color.black)
             }.onChange(of: vm.selectedItemsTest) { newValue in
                 print("print selected")
             }

解决办法:

  • 确认绑定变量类型:vm.selectedItemsTest必须是[PhotosPickerItem]数组类型,Xcode 15对这个类型的校验更严格,非数组类型会直接导致多选逻辑异常。
  • 排查onChange回调逻辑:如果后续在这个回调里对selectedItemsTest做了赋值操作,会触发双向绑定的循环更新,直接导致选中状态被取消。当前代码里的print没问题,但要避免任何修改绑定变量的操作。
  • 清理Xcode缓存:升级后残留的Derived Data可能引发奇怪的兼容性问题,清理后重启Xcode再测试。
  • 验证selectionBehavior:确保selectionBehavior设置为.continuous,这是支持多选的正确行为模式,虽然代码里已经设置,但可以显式确认没有被其他逻辑覆盖。

内容的提问来源于stack exchange,提问作者Mustafa Kemal AKPINAR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 15:00:01