小编Hon*_*ney的帖子

Swift扩展 - 必须在非专用泛型类型'Array'上声明约束扩展

我有一个返回JSON对象数组的API.我已将结构设置为如下所示:

typealias MyModels = [MyModel]

struct MyModel: Codable {
    let field1: String
    let field2: String
    let mySubModel: SubModel?

    enum CodingKeys: String, CodingKey {
        case field1 = "Field1"
        case field2 = "Field2"
        case mySubModel = "MySubModel"
    }
}

struct SubModel: Codable {
    let subModelField1: String
    let subModelField2: String

    enum CodingKeys: String, CodingKey {
        case subModelField1 = "SubModelField1"
        case subModelField2 = "SubModelField2"
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是添加此扩展,提供路径var(NetworkModel协议为API操作提供了一些基本功能):

extension MyModels: NetworkModel {
    static var path = "the/endpoint/path"
}
Run Code Online (Sandbox Code Playgroud)

当base是对象或json键时,我在这种方式设置的其他模型/结构类中没有任何问题.但是,由于这个是不同的,并且只是一个数组,当我在该类中放入该扩展时,我收到此错误:

Constrained extension must be declared on …
Run Code Online (Sandbox Code Playgroud)

collections ios type-alias swift swift-extensions

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

如何使用 dispatchQueues 创建参考循环?

我觉得我一直误解了何时创建引用循环。在我过去认为几乎任何你有一个块并且编译器强迫你写的地方之前,.self这表明我正在创建一个引用循环,我需要使用[weak self] in.

但以下设置不会创建参考循环。

import Foundation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution


class UsingQueue {
    var property : Int  = 5
    var queue : DispatchQueue? = DispatchQueue(label: "myQueue")

    func enqueue3() {
        print("enqueued")
        queue?.asyncAfter(deadline: .now() + 3) {
            print(self.property)
        }
    }

    deinit {
        print("UsingQueue deinited")
    }
}

var u : UsingQueue? = UsingQueue()
u?.enqueue3()
u = nil
Run Code Online (Sandbox Code Playgroud)

该块仅保留self3 秒。然后释放它。如果我使用async而不是asyncAfter那么它几乎是立即的。

据我了解,这里的设置是:

self ---> queue
self <--- block
Run Code Online (Sandbox Code Playgroud)

队列只是块的外壳/包装器。这就是为什么即使我nil排队,块也会继续执行。他们是独立的。

那么是否有任何设置只使用队列并创建参考循环?

据我所知[weak self],仅用于引用循环以外的其他原因,即 …

closures memory-leaks memory-management grand-central-dispatch swift

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

如何量化主线程上的等待块数?

我继承了一些代码,可以将任何操作抛出到主 UI 线程上。例如,当网络操作返回时,完成块会自动在主线程上运行,并且程序员会无意识地将 UI 代码与网络请求混合在一起。

我如何量化主 UI 线程上的负载或压力,以便我可以向经理证明将所有内容都放在主线程上是一种不好的做法?我希望以编程方式获取此数据,因为我也想记录它。

multithreading objective-c instruments ios swift

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

为什么我收到“无法获取键控解码容器 - 而是找到了空值。” 当我使用 JSONDecoder 时出错

($10741e078 处的未知上下文).CodingKeys>,Swift.DecodingError.Context(codingPath:[],debugDescription:“无法获取键控解码容器 - 找到空值。”,underlyingError:nil)

我的模型是,

struct Details : Decodable {

    var userInfo : RequestUserInfo
    var commercialInfo : String?

}

struct RequestUserInfo : Decodable {

    var UserName : String?
    var Email : String?
    var UserIdentity : String?
    var UserMobile : String?
    var ThirdName : String?
    var LastName : String?
    var IdIssueDate : String?
    var IdSourceCity : String?


}
Run Code Online (Sandbox Code Playgroud)

($10741e078 处的未知上下文).CodingKeys>,Swift.DecodingError.Context(codingPath:[],debugDescription:“无法获取键控解码容器 - 找到空值。”,underlyingError:nil)

json ios swift decodable jsondecoder

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

如何使用 addObserver 关闭方法在 Swift 5 中删除观察者

这是我的第一篇文章。我是日本的 iOS 工程师(这个月刚入职)。

我在 Swift 5 中的removeObserver方法有问题NotificationCenter

我使用闭包类型向 ViewController (VC) 添加了观察者addObserver。当 VC 的取消初始化调用时,我想删除这个观察者。

NotificationCenter.default.removeObserver(self)用VC的deinit方法写的。但是,这似乎对我不起作用。

有什么问题???

此外,如果我的代码有内存泄漏问题,请告诉我如何修复它。

这是我的一段代码。

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { [weak self] notification in

            guard let self = self else { return }
            self.loadWeather(notification.object)
        }
    }
    
    deinit {
        print(#function)
        print("ViewController died")

        NotificationCenter.default.removeObserver(self)
    }
}
Run Code Online (Sandbox Code Playgroud)

memory-management nsnotificationcenter ios swift swift5

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

通知服务扩展是否需要任何功能?

I\xe2\x80\x99m 为通知服务扩展创建应用程序 ID

\n\n

我需要为其添加任何功能吗?\n其唯一目的是下载通知图像。我认为我不需要\xe2\x80\x99。

\n\n

目前,我可以在不启用 Xcode 中任何功能的情况下构建到我的设备中。尽管当我查看 Xcode 自身构建的配置文件时,我看到钥匙串共享已启用。

\n\n

我还看到了本教程,该教程与启用“应用程序组”有关。\n其他教程涉及启用应用程序组和推送通知。我认为我不需要应用程序组,因为我不需要传递任何值,只需更新通知即可。

\n\n

我在文档中没有找到任何提到正确方法的内容。一位朋友告诉我,他在没有启用任何功能的情况下就开始工作,所以我很困惑。

\n

xcode ios provisioning-profile ios-app-extension notification-service-extension

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

Xcode 11 苹果发行版已被接受,但无法使用 Flutter 构建

我更新了我的 1 年苹果开发并更新了我的证书

> Xcode 11 supports the new Apple Development and Apple Distribution certificate types. These certificates support building, running, and
> distributing apps on any Apple platform. Preexisting iOS and macOS
> development and distribution certificates continue to work, however,
> new certificates you create in Xcode 11 use the new types. Previous
> versions of Xcode don’t support these certificates. (45527608)
Run Code Online (Sandbox Code Playgroud)

由于 Xcode11 的这个注释,我测试创建一个 Apple 发行版而不是像以前一样的 iOS 发行版

所以,我创建了这个并在 Xcode 中选择了好的配置文件。

在这一步,一切都很好。

但是现在当我尝试运行应用程序时

我有

No Provisioning Profile …
Run Code Online (Sandbox Code Playgroud)

xcode provisioning-profile flutter

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

应用程序建立在物理设备上,但不是模拟器(没有这样的模块)

我的项目将建立在物理设备上,而不是建立在模拟器上。建立在模拟器上说没有这样的模块。我已经安装了所有必要的 pod,但是在尝试在模拟器上构建时无法访问它们。Intellisense 不会接收它。

在此处输入图片说明

虽然,我肯定已经多次安装和更新了 pod,甚至尝试使用过时的版本但没有运气

在此处输入图片说明

我尝试删除和分解 pod、删除 DerivedData、删除 .xcworkspace 文件、清理构建文件夹。我似乎无法在模拟器上构建它,因为“没有这样的模块”。

iOS 部署目标是 13.2,而模拟器运行的是最新版本(14.2)

更新:这似乎发生在我安装的任何 pod 上……Stripe、GoogleMaps 等。

通过拖放所需的文件夹将 Firebase 框架直接下载并导入到项目中似乎摆脱了 Firebase 错误,但其他模块仍然不起作用:Stripe、GoogleMaps 等。

xcode ios cocoapods firebase firebase-authentication

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

为什么UITableViewCell有一个contentView属性?

这个contentView属性有什么意义?我的意思是:为什么不是所有的子视图都添加到了self

让我说对了:每个单元格都是一个视图(UITabvleViewCell是一个UIView子类).而这个胖视图有另一个胖视图,其上有相同的边界,名为contentView.然后,contentView会携带所有其他子视图.

现在他们为什么不保存那些额外的内存?这个决定背后有什么天才逻辑吗?很想明白这个的原因.

uitableview ios uicollectionview

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

将Storyboard转换为XIB

我使用iOS 5 Storyboard功能创建了一个应用程序,并决定我也想在iOS 4上运行它.如何将我的Storyboard(只包含一个ViewController)转换为XIB?还有,ARC会为iOS 4编译吗?

xcode objective-c storyboard xib ios

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