小编Hop*_*ope的帖子

使用Swift 3停止scheduledTimer,即使timer为零,Timer也会继续触发

我们调用startTimer函数来启动计时器.当我们想要停止它时,我们调用stopTimerTest函数,但在我们调用stopTimer函数后,timerTestAction继续触发.要检查计时器条件,我们在timerActionTest中使用print和print返回nil.

var timerTest: Timer? = nil

func startTimer () {
    timerTest =  Timer.scheduledTimer(
        timeInterval: TimeInterval(0.3),
        target      : self,
        selector    : #selector(ViewController.timerActionTest),
        userInfo    : nil,
        repeats     : true)
}

func timerActionTest() {
    print(" timer condition \(timerTest)")
}

func stopTimerTest() {
    timerTest.invalidate()
    timerTest = nil
}
Run Code Online (Sandbox Code Playgroud)

timer swift3

28
推荐指数
4
解决办法
4万
查看次数

Swift,NSJSONSerialization和NSError

问题是当数据不完整NSJSONSerialization.JSONObjectWithData导致应用程序崩溃unexpectedly found nil while unwrapping an Optional value而不是通知我们使用NSError变量时.所以我们无法阻止崩溃.

您可以在下面找到我们正在使用的代码

      var error:NSError? = nil

      let dataToUse = NSJSONSerialization.JSONObjectWithData(receivedData, options:   NSJSONReadingOptions.AllowFragments, error:&error) as NSDictionary

    if error != nil { println( "There was an error in NSJSONSerialization") }
Run Code Online (Sandbox Code Playgroud)

直到现在我们都无法找到解决方法.

nserror nsjsonserialization swift

17
推荐指数
2
解决办法
3万
查看次数

您的应用程序在连接到IPv6网络的运行iOS 11.2.5的iPad或iPhone上崩溃,但为什么?

使用此消息拒绝的应用程序版本之一您的应用程序在连接到IPv6网络的运行iOS 11.2.5的iPad或iPhone上崩溃.问题是应用程序没有单一的网络功能或功能,所以我无法理解发生了什么.

这是应用程序的第4版,旧版本已经存储.在最后一个版本中,我们刚刚删除了一个问题,导致应用程序在新的iOS版本中失败文本.

ipv6 ios

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

升级 Xcode 版本 14.0 后,我开始在控制台中获取 espresso 消息

我将 Xcode 升级到 14.0,将测试设备 iOS 升级到 16.0,然后我开始在控制台中收到我的一个 SwiftUI 项目的消息。

它看起来像下面的代码。每次这部分调用时,我们都会收到消息。

  let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: cmBuffer, orientation: CGImagePropertyOrientation(rawValue: 6)!, options: requestOptions)
    
   
    
    do {
        
        try imageRequestHandler.perform(self.requests)
    
    } catch {
        
        print(error)
        
    }
Run Code Online (Sandbox Code Playgroud)

[1235:196713] [espresso] [Espresso::handle_ex_plan] 异常=espresso_plan 的阶段不一致:控制台中的 0 status=-5 消息。

我找不到为什么会这样以及如何避免。

xcode ios swift swiftui

6
推荐指数
2
解决办法
877
查看次数

如何在Swift中初始化CVPixelBufferRef

我们曾经如下初始化CVPixelBufferRef.

  CVPixelBufferRef pxbuffer = NULL;
Run Code Online (Sandbox Code Playgroud)

但是在Swift中我们不能使用NULL,所以我们尝试了以下但当然XCODE希望我们将它用于初始化以使用它

let pxbuffer: CVPixelBufferRef
Run Code Online (Sandbox Code Playgroud)

但怎么样?

在Obj_C中,我们正在创建这样的缓冲区,但正如我在转换为Swift时尝试解释的那样,我已经在第一行停止了.

 CVPixelBufferRef pxbuffer = NULL;

CVPixelBufferCreate(kCFAllocatorDefault, picGenislik,
                    frameHeight, kCVPixelFormatType_32ARGB, (__bridge         CFDictionaryRef) options,
                    &pxbuffer);
Run Code Online (Sandbox Code Playgroud)

initialization swift

5
推荐指数
2
解决办法
4322
查看次数

工作 MPMediaPickerController 函数开始出现错误,但为什么?

我正在使用以下内容在我的应用程序中选择音频,但开始出现错误,但现在不能。有没有人有同样的问题。

设置我正在使用的选择器

func selectAudioFromIpod() {
   
    let picker = MPMediaPickerController.init(mediaTypes:MPMediaType.anyAudio)
    picker.delegate = self
    picker.allowsPickingMultipleItems = false
    present(picker, animated: true, completion: nil)

}
Run Code Online (Sandbox Code Playgroud)

并以此获得媒体

func mediaPicker(_ mediaPicker: MPMediaPickerController,
                 didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
    
    dismiss(animated: true, completion: { [self] in
   
        let selectedAudio : NSArray = (mediaItemCollection.items as NSArray?)!
        let audioItem    = selectedAudio.object(at: 0)

        audioURL_iPOD = (audioItem as AnyObject).value(forProperty:  MPMediaItemPropertyAssetURL) as? URL
        
        
        if audioURL_iPOD != nil {
            
          
            audioAsset = AVAsset(url: audioURL_iPOD!) as AVAsset
            
            
        } else { // we could not pick audio from media library …
Run Code Online (Sandbox Code Playgroud)

swift

5
推荐指数
1
解决办法
1481
查看次数

滚动到 SwiftUI Picker 选择列表中的所选项目

我有以下选择器。问题是选择器显示的列表是可滚动的,当所选值接近数组末尾时,用户必须手动滚动。有没有办法通过设置自动滚动到所选项目。

                        Menu {
                            Picker("", selection: $selectedItem) {
                                ForEach(0..<myArray.count, id: \.self) {index in
                                    Text(myArray[index])
                                }  
                            }
                        } label: {
                            HStack(spacing: 1) {
                                Text("Selection Name")
                                    .foregroundColor(.white)
                                    .frame(width: myWidth, height: myHeight, alignment: .trailing)
                                Text(myArray[$selectionIndex])
                                .frame(width: myWidth, height: myHeight, alignment: .center)
                                .multilineTextAlignment(.leading)
                                .background(Color.white)
                                .foregroundColor(.black)
                                .cornerRadius(5)
                                Text(myAffix)
                                    .foregroundColor(.white)
                                    .frame(width: myWidth, height: myHeight, alignment: .leading)
                                
                            }
                            .foregroundColor(.white)
                        } 
Run Code Online (Sandbox Code Playgroud)

swift swiftui

5
推荐指数
0
解决办法
303
查看次数

如何使用 Swift 更改工具栏按钮项标题颜色

我们正在使用 Obj-C 更改工具栏按钮项目标题颜色

    [_anyToolbarButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor lightGrayColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

但在使用 Swift 时我无法改变。

colors toolbar title swift

4
推荐指数
1
解决办法
2843
查看次数

如何删除编辑完成时选择的 NSTextField 的指示器

当选择一个可编辑的 NSTextField 时,Cocoa 通过在文本字段周围制作一个蓝色矩形将其标记为默认值。

但是,编辑结束后,除非选择了另一个文本字段,否则无法删除指示器。

要删除选择指示器我们应该做什么?

我们使用以下函数来检测编辑的结束。

 override func controlTextDidEndEditing(notification: NSNotification) {

   // doing things like getting the string user has been made.

   }
Run Code Online (Sandbox Code Playgroud)

macos cocoa nstextfield swift

4
推荐指数
2
解决办法
3085
查看次数

检测用户是否打开或关闭了Wifi或蓝牙

我们如何确定使用Swift语言打开/关闭了蓝牙或Wifi?

我的应用程序使用蓝牙或Wifi与其他设备进行通信。这些通讯没有问题,但是我们想通知用户Wifi和/或蓝牙是否关闭(当用户使用该应用程序时)。我无法在Swift中做到这一点。

bluetooth wifi swift

3
推荐指数
1
解决办法
1万
查看次数

Swift 3和无法识别的选择器发送到实例错误以创建按钮

在我们开始使用Swift 3后,我们遇到了这个'无法识别的选择器发送到实例!错误,无法在合理的时间找到解决方案.我们在旧版Swift中遇到的所有答案.这是我们提出的行动宣言

     testbutton.addTarget(self, action: Selector("testButtonAction:"), for: UIControlEvents.touchDown)
Run Code Online (Sandbox Code Playgroud)

这是Xcode的"更正"版本,但它们都没有工作.

     testbutton.addTarget(self, action: Selector(("testButtonAction:")), for: UIControlEvents.touchDown)
Run Code Online (Sandbox Code Playgroud)

这是动作功能.

       func testButtonAction(sender: UIButton!) {

            print("Button tapped")

          }
Run Code Online (Sandbox Code Playgroud)

uibutton swift ios10

2
推荐指数
1
解决办法
5045
查看次数

如何使用 Swift 创建具有 kCVPixelFormatType_32BGRA 格式缓冲区的 UIImage

我们使用以下代码从缓冲区创建 UIImage

let context = CGContext(data: baseAddress, width: frameWidth, height: frameHeight, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!

    let cgImage0 =   context.makeImage()  
    let uiImage0 =  UIImage(cgImage: cgImage0!) 
Run Code Online (Sandbox Code Playgroud)

问题是我们只能使用 AVCaptureVideoDataOutput() 以 kCVPixelFormatType_32BGRA 格式创建彩色图像缓冲区,这是与 kCVPixelFormatType_32RGBA 最相似的格式

在某些时候,我们需要 UIImage,并且需要将缓冲区转换为 UIImage,并且因为 UIImage 是 RGBA 格式,所以颜色已变得扭曲。

我们如何从缓冲区中获取 kCVPixelFormatType_32RGBA 格式的 UIImage。

cgcontext ios swift

2
推荐指数
1
解决办法
1783
查看次数