标签: swift5

Swift 5应用程序是否只能在特定的iOS版本上运行?

最近我读了一些关于Swift 5是ABI Stable的文章(这基本上意味着你不需要在应用程序包中打包应用程序自己的Swift动态库版本),这里有一个令人困惑的部分:

因为Swift将嵌入iOS操作系统中.

现在听起来像Swift Dynamic Library现在可以直接进入iOS.这是否意味着编译Swift 5的Apps将只能在特定的iOS版本上运行?

abi ios swift swift5

8
推荐指数
1
解决办法
279
查看次数

Swift Property Wrapper是否可以引用其包装的财产所有者?

从Swift的属性包装器中,您可以有人引用拥有被包装属性的类实例或被删除的实例吗?使用self显然不起作用,也不起作用super

我试图传递self给属性包装器,init()但这也不起作用,因为self在评估Configuration时on 尚未定义@propertywrapper

我的用例在用于管理大量设置或配置的类中。如果有任何属性更改,我只想通知感兴趣的人某些更改。他们实际上并不需要只知道哪个值,因此实际上不必为每个属性使用KVOPublisher

属性包装器看起来很理想,但是我不知道如何传递对包装器可以回调的拥有实例的某种引用。

参考文献:

SE-0258

enum PropertyIdentifier {
  case backgroundColor
  case textColor
}

@propertyWrapper
struct Recorded<T> {
  let identifier:PropertyIdentifier
  var _value: T

  init(_ identifier:PropertyIdentifier, defaultValue: T) {
    self.identifier = identifier
    self._value = defaultValue
  }

  var value: T {
    get {  _value }
    set {
      _value = newValue

      // How to callback to Configuration.propertyWasSet()?
      //
      // [self/super/...].propertyWasSet(identifier)
    }
  } …
Run Code Online (Sandbox Code Playgroud)

swift swift5 combine

8
推荐指数
2
解决办法
696
查看次数

如何解决“模型的概述消耗?” Swift 5 运行时崩溃

我刚刚将我的一个应用程序升级到 Swift 5,我根本没有进行太多更改,并且似乎没有任何问题,所以我只是在生产中发布了它(幸运的是在分阶段发布)。1 天后,我开始在 Crashlytics 上看到一个非常奇怪的崩溃,影响了 15% 使用我的应用程序的人。

这是堆栈跟踪:

Crashed: com.apple.main-thread
0  (Missing)                      0x31698aa1503e0 (Missing)
1  libswiftCore.dylib             0x1afa5ac68 _swift_release_dealloc + 28
2  App                            0x104194c3c outlined consume of MyModel? + 4300033084
3  App                            0x104482cb8 @objc MyController.__ivar_destroyer (<compiler-generated>)
4  libobjc.A.dylib                0x180f267cc object_cxxDestructFromClass(objc_object*, objc_class*) + 148
5  libobjc.A.dylib                0x180f366b8 objc_destructInstance + 68
6  libobjc.A.dylib                0x180f36720 object_dispose + 16
7  UIKitCore                      0x1ae2edac0 -[UIResponder dealloc] + 152
8  UIKitCore                      0x1add173e0 -[UIViewController dealloc] + 1748
9  App                            0x10431d82c BaseViewController.__deallocating_deinit (MyController.swift:56)
10 App                            0x10431d85c @objc BaseViewController.__deallocating_deinit (<compiler-generated>) …
Run Code Online (Sandbox Code Playgroud)

swift swift5

7
推荐指数
1
解决办法
3796
查看次数

Swift:快速/敏捷运行异步测试显示错误:“InvalidNimbleAPIUsage”,原因:“expect(...).toEventually(...) 只能在主线程上运行。”

我正在使用 Quick/Nimble 发出网络请求 (URLRequest) 来实现一个测试用例,但出现此错误:

 *** Terminating app due to uncaught exception 'InvalidNimbleAPIUsage', reason: 'expect(...).toEventually(...) can only run on the main thread.'
Run Code Online (Sandbox Code Playgroud)

这是我的测试用例:

 expect(someVar).toEventually(equal("bar"), timeout: 0.2, pollInterval: 0.1, description: "time")
Run Code Online (Sandbox Code Playgroud)

如果改变我的实现:

DispatchQueue.main.async {
    expect(someVar).toEventually(equal("bar"), timeout: 0.2, pollInterval: 0.1, description: "time")
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

*** 由于未捕获的异常“InvalidNimbleAPIUsage”而终止应用程序,原因:“不允许嵌套异步期望以避免创建片状测试。

你们中的任何人都知道如何使用此测试用例来工作吗Quick/Nimble

我非常感谢你的帮助

quick-nimble ios10.3 xcode10 swift5

7
推荐指数
0
解决办法
1560
查看次数

由于未决请求错误,CoreData 在 MacOS 应用程序上将数据同步到云时出现问题

我正在为 MacOS 编写一个应用程序,主要功能是将数据同步到 iCloud。我已经完成的所有工作就是 - 打开 Core-Data 和 CloudKit 选项的项目, - 创建一个模型和一些基本例程,通过将文件导入到应用程序来创建模型。

由于这是一个大文件,因此我在单独的线程/调度新队列中执行所有操作。当我调试应用程序时,将数据保存到本地数据库工作正常,然后当应用程序失去焦点时数据会同步到云,我不得不说它有效,我可以通过查询 CloudKit 仪表板中的数据来说明这一点。但是,当我开始导入文件以及在导入过程中我将切换到其他应用程序时,同步数据存在问题。导入完成后,我看到数据存储在本地,但无法同步到云端。相反,我在控制台中看到以下错误:

CoreData: 调试: CoreData+CloudKit: -PFCloudKitThrottledNotificationObserver noteRecievedNotification:: : 得到: NSApplicationWillBecomeActiveNotification - 0 CoreData: 调试: CoreData+CloudKit: -PFCloudKitThrottledNotificationObserver noteRecievedNotification:: : 得到: NSApplicationDidBecomeActiveNotification - 1 CoreData: CloudKit: CoreData+CloudKit: -PFCloudKitThrottledNotificationOb服务器注释已收到通知: : - 已经安排了一个块来响应“NSApplicationDidBecomeActiveNotification”,此后有 2 个通知。CoreData: debug: CoreData+CloudKit: -[PFCloudKitThrottledNotificationObserver noteRecievedNotification:]_block_invoke(49): : 为“NSApplicationWillBecomeActiveNotification”执行“AppActivateObserver”块,清除 2 次迭代。CoreData: CloudKit: CoreData+CloudKit: -NSCloudKitMirroringDelegate _scheduleAutomatedExportWithLabel:activity:completionHandler:: - 开始自动导出 - AppActivationExport: (null) CoreData: CloudKit: CoreData+CloudKit: -NSCloudKitMirroringDelegateexecuteMirroringRequest:error:: : 要求执行请求:41AA55D5- F8A9-4EA9-A12A-9CA05775EC03 CoreData: CloudKit: CoreData+CloudKit: -NSCloudKitMirroringDelegate _scheduleAutomatedImportWithLabel:activity:completionHandler:: - 开始自动导入 - AppActivationImport - 响应活动:(null) CoreData: …

core-data ios swift cloudkit swift5

7
推荐指数
0
解决办法
1028
查看次数

在Limited Library Picker上配置选择限制

考虑到用户隐私,我想实现一个有限的库选择器(在 iOS 14 中引入)。我们的应用程序允许用户为每个组件附加一张照片,从而处理技术组件的状况评估。为此,我们已经使用了PHPickerViewControllerselectionLimit 为1 的传统配置。我希望新的Limited Library Picker 能够提供类似的PHPickerConfiguration选项。然而,情况似乎并非如此,因为它的 API 没有公开可配置PHPickerViewController对象。presentLimitedLibraryPicker相反,呈现有限库选择器的唯一方法显然是从单例中调用该方法PHPhotoLibrary。因此,我始终可以在“有限库”视图中选择多张照片,而所需的行为是最多选择一张照片。有谁知道如何使用有限的库选择器来实现这种行为?

在下面的代码示例中,如果用户PHAuthorizationStatus.authorized(工作正常),则显示传统的 PHPickerViewController;如果PHAuthorizationStatus.limited(缺少选择限制),则显示有限库选取器:

        PHPhotoLibrary.requestAuthorization(for: .addOnly) { status in
            switch status {
            case .authorized:
                var configuration = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
                configuration.filter = .images
                configuration.selectionLimit = 1
                let phPicker = PHPickerViewController(configuration: configuration)
                phPicker.delegate = self as PHPickerViewControllerDelegate
                DispatchQueue.main.async {
                    self.view?.present(viewController: phPicker)
                }
            case .limited:
                PHPhotoLibrary.shared().register(self)
                DispatchQueue.main.async {
                    PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
                }
            default:
                //Ask for permission
        }
Run Code Online (Sandbox Code Playgroud)

xcode ios swift swift5 ios14

7
推荐指数
0
解决办法
1258
查看次数

Swift async/await 相当于 Promise Kit“when”模式

我是 Swift 的新手,来自 JS,我已经开始构建一个 iOS 应用程序。

最初,我使用 Promise Kit 来处理异步内容,因为它对我来说比我读到的其他内容更容易。

无论如何,在 JS 中,我经常使用以下模式:

async function doAyncFunction(item) {
  try {
    // do async call to fetch data using item
    return Promise.resolve(data);
  } catch (error) {
    return Promise.reject(error);
  }
}
const promises = items.map((item) => doAyncFunction(item));
const results = await Promise.all(promises);
Run Code Online (Sandbox Code Playgroud)

我最终通过 Promise Kit 实现了这一点,如下所示:

func doManyAsyncRequests(userIds: [String], accessToken: String) -> Promise<Void> {
  Promise { seal in
    let promises = spotifyUserIds.map {
      doSingleAsyncRequest(userId: $0.id, accessToken: accessToken) // this function returns a promise …
Run Code Online (Sandbox Code Playgroud)

swift promisekit swift5 ios15

7
推荐指数
1
解决办法
3114
查看次数

如何在 swift 中使用 async/await 同时运行多个 API 调用?

根据 Swift 中新的结构化并发,我们可以等待在异步上下文中完成的 API 调用,但我想同时进行多个 API 调用,例如,目前如果一个接一个地添加 API 调用,

profileData = await viewModel.getProfileData()
userCardData = await viewModel.getCardDetails()
Run Code Online (Sandbox Code Playgroud)

上述情况的问题是只有在获取配置文件数据后才获取 CardDetails。

ios async-await swift swift5

7
推荐指数
1
解决办法
6162
查看次数

多点连接 - 在 Swift 5 中获取文件传输(互联网)速度和文件大小

我正在点对点传输照片。一切正常,但我无法获得照片(文件)传输速度和互联网速度。与MB 一样,文件也被传输。其次,我想获取该文件的大小。

我们使用数据格式传递照片MCSession

由于隐私原因,我无法在此处添加项目代码,但我将分享我关注的参考 github 项目。在项目中,我传递字符串,在我的例子中传递照片。所有的事情都是一样的。

我在 Stackoverflow 上查了一下,但没有找到任何准确的答案!

参考项目链接: https://github.com/YogeshPateliOS/MultipeerConnectivity-.git

谢谢你!

wifi multipeer-connectivity mcsession swift swift5

7
推荐指数
1
解决办法
1439
查看次数

如何将 Swift 宏与 @Observable 和 @Environment 一起使用?

我正在尝试通过以下代码使用 @Observable 和 @Environment 来使用新的 Swift 宏:

import SwiftUI
import Observation

@Observable class Note {
    var text = ""
}

struct ContentView: View {
    @State var note = Note()
    
    var body: some View {
        FormView()
            .environment(note)
    }
}

struct FormView: View {
    @Environment(Note.self) var note
    
    var body: some View {
        Form {
            TextField("write here", text: $note.text)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,无法构建并出现以下错误:

Cannot find '$note' in scope

删除$in 的$note结果是:

Cannot convert value of type 'String' to expected argument …

xcode swift swift5 swiftui swift-macro

7
推荐指数
1
解决办法
1265
查看次数