小编PGD*_*Dev的帖子

如何在Codable类型中使用Any

我目前正在处理Codable项目中的类型并遇到问题.

struct Person: Codable
{
    var id: Any
}
Run Code Online (Sandbox Code Playgroud)

id在上面的代码中可以是a String或an Int.这就是id类型的原因Any.

我知道Any不是Codable.

我需要知道的是我如何才能使它发挥作用.

ios swift codable decodable encodable

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

Swift 3.0中的NSIndexPath和IndexPath

我最近将我的代码转换为Swift 3.0.我的集合视图和表视图数据源方法现在包含IndexPath而不是NSIndexPath在其方法签名中.但仍然在方法定义中,它是对NSIndexPath的类型转换IndexPath.即

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell : NNAmenitiesOrFurnishingCollectionViewCell = self.amenitiesOrFurnishingCollectionView.dequeueReusableCell(withReuseIdentifier: "NNAmenitiesOrFurnishingCollectionViewCell", for: indexPath) as! NNAmenitiesOrFurnishingCollectionViewCell
        cell.facilityImageName = self.facilityArray[(indexPath as NSIndexPath).row].imageName
        cell.facilityLabelString = self.facilityArray[(indexPath as NSIndexPath).row].labelText
        return cell
    }
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我为什么indexPath类型铸造NSIndexPath.

ios uicollectionview swift swift3

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

为不同的UITableViewCell类创建单个.xib

我正面临一个问题

我有一个NNVerticalStackTableViewCell.xib文件及其相应的NNVerticalStackTableViewCell.swift文件.

NNVerticalStackTableViewCell.swift文件代码

class NNVerticalStackTableViewCell: UITableViewCell
{
    //MARK: Outlets
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var verticalStack: NNVerticalStackView!

    //MARK: Internal Properties
    var titleString : String?
        {
        set{
            self.titleLabel.text = newValue
        }
        get{
            return self.titleLabel.text
        }
    }

    //MARK: View Lifecycle Methods
    override func awakeFromNib()
    {
        super.awakeFromNib()
        self.titleLabel.text = nil
    }

    //MARK: Internal Methods
    func addViewsInVerticalStack(viewsArray : [UIView])
    {
        for view in viewsArray
        {
            self.verticalStack.insertStackItem(view)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

NNVerticalStackTableViewCell.xib文件界面

在此输入图像描述

class nameas:NNVerticalStackTableViewCell和 …

uitableview xib ios swift

14
推荐指数
1
解决办法
2065
查看次数

在Swift 4中,无法覆盖扩展声明

我最近将我的代码迁移到了Swift 4.我面临扩展问题,即

来自扩展的声明无法覆盖

我已经阅读了多个重新发布此问题的帖子.但他们都没有接受下面描述的情景:

class BaseCell: UITableViewCell
{
    //Some code here...
}

extension BaseCell
{
    func isValid() -> String?
    {
        //Some code here...
    }
}

class SampleCell: BaseCell
{
    //Some code here...

    override func isValid() -> String? //ERROR..!!!
    {
        //Some code here...
    }
}
Run Code Online (Sandbox Code Playgroud)

根据Apple的说法,

扩展可以为类型添加新功能,但它们不能覆盖现有功能.

但在上面的场景中,我没有覆盖isValid()扩展中的方法.它在SampleCell类定义本身中被重写.不过,它正在给出错误.

extension-methods ios swift swift4

13
推荐指数
3
解决办法
9742
查看次数

如何使用Kingfisher仅在磁盘中缓存图像?

我正在使用Kingfisher库来下载和缓存图像.我在实施中面临一些问题:

  1. 图像是否缓存在内存和磁盘中?

  2. 是否有任何规定只在磁盘上缓存图像?

我已经阅读了多篇关于此的帖子,但找不到任何解决方案.

caching image-caching ios swift kingfisher

8
推荐指数
3
解决办法
3909
查看次数

NSAttributedString从HTML暗模式初始化,如何设置默认前景色?

在将现有应用程序转换为深色模式时,我偶然发现了一个UITextView带有 an的初始化NSAttributedString,用于为某些应用程序商店法律术语提供精美印刷。这在浅色模式下看起来不错,但在深色模式下我会在黑色上看到黑色文本。我提供默认前景色的第一个想法是添加以下内容:

import UIKit

let fgColor: UIColor
if #available(iOS 13.0, *) {
    fgColor = .label
} else {
    fgColor = .black
}
let s = try NSAttributedString(data: NSLocalizedString("""
        <p style="text-align: center; font-size: smaller;">
        Your payment will be charged to your iTunes Account at confirmation of purchase. Your subscription automatically renews unless auto-renew is turned off at least 24-hours before the end of the current period. Your Account will be charged for renewal within 24-hours prior to …
Run Code Online (Sandbox Code Playgroud)

html nsattributedstring ios swift

6
推荐指数
1
解决办法
2205
查看次数

swift中的变异结构函数是否会创建一个新的self副本?

我喜欢swift中的值语义,但我担心变异函数的性能.假设我们有以下内容struct

struct Point {
   var x = 0.0
   mutating func add(_ t:Double){
      x += t
   }
}
Run Code Online (Sandbox Code Playgroud)

现在假设我们创建一个Point并将其变异为:

var p = Point()
p.add(1)
Run Code Online (Sandbox Code Playgroud)

现在内存中的现有结构变异,或者被struct替换为新的实例

self = Point(x:self.x+1)
Run Code Online (Sandbox Code Playgroud)

performance struct swift swift-structs

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

带有动态键的Swift Codable

我有JSON结构为:

"periods": {
    "2018-06-07": [
      {
        "firstName": "Test1",
        "lastName": "Test1"
      }
    ],
    "2018-06-06": [
      {
        "firstName": "Test1",
        "lastName": "Test1"
      }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我试图这样解析:

public struct Schedule: Codable {
    public let periods: Periods
}

public struct Periods: Codable {
    public let unknown: [Inner]

    public struct Inner: Codable {
        public let firstName: String
        public let lastName: String
    }

    private struct CustomCodingKeys: CodingKey {
        var stringValue: String
        init?(stringValue: String) {
            self.stringValue = stringValue
        }

        var intValue: Int?
        init?(intValue: Int) {
            return …
Run Code Online (Sandbox Code Playgroud)

json swift swift4 codable

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

Swift 中的 ArraySlice 如何在内部工作?

我已经阅读了多篇关于如何在 Swift 中ArraySlice使用的帖子和文章Array

但是,我找不到的是它在内部是如何工作的?是什么ArraySlice是浏览到数组究竟是什么意思?

var arr = [1, 2, 3, 4, 5]
let slice = arr[2...4]

arr.remove(at: 2)
print(slice.startIndex) //2
print(slice.endIndex)   //5
slice[slice.startIndex] //3
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我index-2 (i.e 3)从 from 中删除了元素arrIndex-2startIndexslice为好。当我打印时,slice[slice.startIndex]它仍然打印 3。

既然没有为 创造额外的存储空间ArraySlice,那么为什么 中的任何变化Array都没有反映在 中ArraySlice

文章/帖子可以在这里找到:

https://dzone.com/articles/arrayslice-in-swift

https://marcosantadev.com/arrayslice-in-swift/

arrays swift

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

Swift中多线程环境中的写时复制

我已经阅读了有关在中进行优化Arrays和其他数据结构的写时复制概念Swift

我想知道的是写时复制在多线程环境中如何工作。

    let arr1 = [1, 2, 3, 4]
    let arr2 = arr1
    arr1.withUnsafeBytes { print("arr1:", $0.baseAddress) } //0x000060000007ee60
    arr2.withUnsafeBytes { print("arr2:", $0.baseAddress) } //0x000060000007ee60

    DispatchQueue.global(qos: .default).async {
        let arr3 = arr1
        arr3.withUnsafeBytes { print("arr3:", $0.baseAddress) } //0x000060000007ee60
    }
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,arr1并且arr2最初具有与中期望的相同的地址copy-on-write。不过,arr3成也共享相同的存储arr1arr2虽然是在不同的线程执行。

据我所知,每个线程都有不同的堆栈分配。那为什么arr3仍然共享相同的位置?

有人可以解释一下它是如何工作的。

arrays copy-on-write ios swift ios-multithreading

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