标签: swift5

在Swift 3.0中生成随机字节

我想SecRandomCopyBytes在Swift 3.0中使用生成随机字节.这是我在Swift 2.2中的表现

private static func generateRandomBytes() -> String? {
    let data = NSMutableData(length: Int(32))

    let result = SecRandomCopyBytes(kSecRandomDefault, 32, UnsafeMutablePointer<UInt8>(data!.mutableBytes))
    if result == errSecSuccess {
        return data!.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
    } else {
        print("Problem generating random bytes")
        return nil
    }
}
Run Code Online (Sandbox Code Playgroud)

在Swift 3中,我尝试这样做,因为我知道unsafemutablebytes的概念现在不同了,但它不允许我返回.如果我评论退货部分,它仍然说Generic Parameter ResultType could not be inferred

fileprivate static func generateRandomBytes() -> String? {
    var keyData = Data(count: 32)
    _ = keyData.withUnsafeMutableBytes {mutableBytes in
        let result = SecRandomCopyBytes(kSecRandomDefault, keyData.count, mutableBytes)
        if result == errSecSuccess { …
Run Code Online (Sandbox Code Playgroud)

ios swift swift3 swift5

12
推荐指数
3
解决办法
6768
查看次数

如何检查视图是否显示在屏幕上?(Swift 5 和 SwiftUI)

我有一个像下面这样的观点。我想知道它是否是屏幕上显示的视图。有没有实现这个的功能?

struct TestView: View {
    var body: some View {
        Text("Test View")
    }
}
Run Code Online (Sandbox Code Playgroud)

xcode ios swift swift5 swiftui

11
推荐指数
3
解决办法
5417
查看次数

应用程序崩溃,Xcode11.4,iOS 10.3.3 10.3.4,iPhone 5c /5 iPad4 (armv7s)

我们的应用程序在 Xcode 11.4 编译的 os 10.3.3 10.3.4、iPhone 5c /5 iPad4 (armv7s 32) 上崩溃,快速优化。我们发现 PC 寄存器指向一个没有虚拟地址和堆栈信息的空地址。如果我们关闭 swift 优化,它会起作用。

那么有没有人找到问题和任何解决方案?

可以肯定,它与Xcode 11.4 swift优化有关。

我在这里找到了同样的问题。https://www.reddit.com/r/iOSProgramming/comments/frcpsc/xcode_114_builds_crashes_on_ios_10/

Incident Identifier: 2224949E-E5E3-479C-9B08-4FD1473144B3
CrashReporter Key:   052c9a28855da965790a6dcc0885097a66ee4eff
Hardware Model:      iPad3,4
Process:             AAAAA [34872]
Path:                /private/var/containers/Bundle/Application/xxxxxx....
Identifier:          com.xxx.xxxxx
Version:             xxxx
Code Type:           ARM (Native)
Role:                Non UI
Parent Process:      launchd [1]
Coalition:           com.xxx.xxxxx [1932]


Date/Time:           2020-03-30 22:42:49.2564 +xxx
Launch Time:         2020-03-30 22:42:47.0000 +xxx
OS Version:          iPhone OS 10.3.3 (14G60)
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Subtype: KERN_PROTECTION_FAILURE …
Run Code Online (Sandbox Code Playgroud)

compiler-optimization swift5 xcode11.4

11
推荐指数
2
解决办法
2031
查看次数

iOS Xcode 12.0 Swift 5 'AppDelegate' 用@main 注释并且必须提供类型为 () -&gt; Void 或 () throws -&gt; Void 的主要静态函数

在我使用 Swift 5 和 Catalina 10.15.5 的 Xcode 12.0 中:当我勾选 Mac 作为可部署目标(Apple Project Catalyst)并单击运行时,它给了我一个错误显示:

'AppDelegate' is annotated with @main and must provide a main static function of type () -> Void or () throws -> Void.
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在互联网上找不到任何东西......我检查了我的代码,没有任何问题,它可以在 iOS 设备和模拟器上运行。调试日志为空(废话...)

有人面临这个问题吗?

macos xcode swift5

11
推荐指数
1
解决办法
2764
查看次数

从 Objective-C 调用 Swift 异步函数

我正在使用新的 Swift 5.5 功能来创建异步函数:

@MainActor
@objc func updateStatus() async {
    /// do async stuff...
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试从 Objective-C 代码调用此方法时,它不会显示在自动完成中,并且如果我尝试构建它,则会出现构建错误:

if (@available(iOS 15.0, *)) {
     CJSubscriptionStatusCheck *backgroundStatusCheck = [[CJSubscriptionStatusCheck alloc] init];
     [backgroundStatusCheck updateStatus]; // No visible @interface for 'CJStatusCheck' declares the selector 'updateStatus'
}
Run Code Online (Sandbox Code Playgroud)

我的其他 swift 代码在 Objective-C 上运行良好,因此设置很好,但我不确定“异步”代码如何与 Objective-C 一起使用(如果有的话)。我无法通过我的研究找到这个问题的答案。

谢谢。

objective-c swift swift5

11
推荐指数
1
解决办法
5017
查看次数

自调整大小的集合视图在 iOS 15 中进入递归循环

我有一个自动调整大小的集合视图,当我调用 super.layoutSubviews 时,我的应用程序崩溃,因为集合视图进入递归更新循环。这在 iOS 14 及更低版本中运行良好。但从 iOS 15 开始就观察到了这一点。

class DynamicCollectionView: UICollectionView {

override var contentSize: CGSize {
    didSet {
        invalidateIntrinsicContentSize()
    }
}

override func layoutSubviews() {
    super.layoutSubviews()
    if bounds.size != intrinsicContentSize {
        invalidateIntrinsicContentSize()
    }
}

override var intrinsicContentSize: CGSize {
    return contentSize
}

override func reloadData() {
    super.reloadData()
    invalidateIntrinsicContentSize()
    layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)

}

崩溃 说:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UICollectionView(<KPFlagship.SelfSizingCollectionView 0x7f896b260e00>)卡在其更新/布局循环中。发生这种情况的原因有很多,包括自调整大小的视图,其首选属性未返回一致的大小。要调试此问题,请检查控制台应用程序中“UICollectionViewRecursion”类别中的日志。

uikit ios swift5 ios15

11
推荐指数
2
解决办法
6623
查看次数

如何在SwiftUI中检测targetEnvironment是否是iPadOS?

我想在为 iOS 和 iPadOS 构建时显示不同的视图。目前,我知道我可以做到

import SwiftUI

struct ContentView: View {
    #if targetEnvironment(macCatalyst)
    var body: some View {
        Text("Hello")
    }
    #else
    var body: some View {
        Text("Hello")
    }
    #endif
}
Run Code Online (Sandbox Code Playgroud)

在 macOS 和 iPadOS/iOS 之间显示不同的视图(在 Swift 4/5 中引入)。但我如何区分后者?我似乎无法使用 targetEnvironment ...

swift5 swiftui

10
推荐指数
3
解决办法
5212
查看次数

将状态/绑定传递给 UIViewRepresentable

将状态变量传递到自定义文本字段的正确方法是什么?我希望避免其他方法/观察结果。这不应该起作用吗?

我在示例项目中重新创建了下面的问题。

import SwiftUI

struct ParentView: View {
    @State var text: String = "initial"
    var body: some View {
        VStack {
            ChildView(text: $text)
            Text(self.text)
        }
    }
}
struct ChildView: View {
    @Binding var text: String
    var body: some View {
        MyTextField(text: $text).frame(width: 300, height: 40, alignment: .center)
    }
}

struct MyTextField: UIViewRepresentable {
    @Binding var text: String
    func makeUIView(context: Context) -> UITextField {
        let view = UITextField()
        view.borderStyle = UITextField.BorderStyle.roundedRect
        return view
    }
    func updateUIView(_ uiView: UITextField, context: Context) { …
Run Code Online (Sandbox Code Playgroud)

ios swift swift5

10
推荐指数
2
解决办法
6970
查看次数

迦太基构建失败 Xcode 12 12A7209 构建

迦太基文件内容:

github "SwiftyJSON/SwiftyJSON"
Run Code Online (Sandbox Code Playgroud)

错误:

Build Failed
Task failed with exit code 1:
/usr/bin/xcrun lipo -create /Users/samrezikram/Library/Caches/org.carthage.CarthageKit/DerivedData/12.0_12A7209/SwiftyJSON/5.0.0/Build/Intermediates.noindex/ArchiveIntermediates/SwiftyJSON\ tvOS/IntermediateBuildFilesPath/UninstalledProducts/appletvos/SwiftyJSON.framework/SwiftyJSON /Users/samrezikram/Library/Caches/org.carthage.CarthageKit/DerivedData/12.0_12A7209/SwiftyJSON/5.0.0/Build/Products/Release-appletvsimulator/SwiftyJSON.framework/SwiftyJSON -output /Users/samrezikram/Downloads/CarthageApp/Carthage/Build/tvOS/SwiftyJSON.framework/SwiftyJSON

This usually indicates that project itself failed to compile. Please check the xcodebuild log for more details: /var/folders/px/jx4gljpj47j06qr7blw6b4w80000gn/T/carthage-xcodebuild.1NVy7o.log
Run Code Online (Sandbox Code Playgroud)

ios react-native swift5 xcode12

10
推荐指数
2
解决办法
5438
查看次数

如何在 Swift 5.5 中将 async/await 与 SwiftUI 一起使用?

我一直在测试 Swift 5.5 版本中预览的异步/等待功能,但我无法从异步函数收集结果并使用 SwiftUI 显示它们。这是我的代码:

import SwiftUI

struct AsyncTestView: View {
    @State var text: String?

    // Async function
    func asyncGetText() async -> String {
        Thread.sleep(forTimeInterval: 10)
        return "My text"
    }
    
    // Stores the result of async function
    func fetchText() async {
        let text = await asyncGetText()
        DispatchQueue.main.async {
            self.text = text
        }
    }
    
    var body: some View {
        Text(text ?? "Loading")
            .onAppear(perform: fetchText)
    }
}
Run Code Online (Sandbox Code Playgroud)

这会导致以下错误:

在不支持并发的函数中调用 'async'
将 'async' 添加到函数 'fetchText()' 以使其异步

添加asyncfetchText()函数然后会导致函数出现以下错误.onAppear() …

async-await swift swift5 swiftui

10
推荐指数
3
解决办法
2740
查看次数