注:有一个类似的问题发布了objective-c 超过here,但我想在swift实现它.

我用swift声明了一个类,如下所示:

import UIKit

class EachDayCell : UITableViewCell
{

    @IBOutlet var dateDisplayLabel : UITextField
    @IBOutlet var nameDisplayLabel : UITextField

    @IBAction func goToPendingItems(sender : AnyObject) {
    }
    @IBAction func showDateSelectionPicker(sender : AnyObject) {
    }

    init(style: UITableViewCellStyle, reuseIdentifier: String!)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
}

现在我想在swift登记中获得一个数组:dateDisplayLabel,nameDisplayLabel.

我怎样才能做到这一点?

推荐答案

Using Mirror

这是一个纯粹的快速解决方案,但有一些局限性:

protocol PropertyNames {
    func propertyNames() -> [String]
}

extension PropertyNames
{
    func propertyNames() -> [String] {
        return Mirror(reflecting: self).children.flatMap { $0.label }
    }
}

class Person : PropertyNames {
    var name = "Sansa Stark"
    var awesome = true
}

Person().propertyNames() // ["name", "awesome"]

限制:

  • 返回Objective-C对象的空数组
  • 不会返回计算(computed)属性,即:

    var favoriteFood: String { return "Lemon Cake" }
    
  • 如果self是一个类的实例(比如,一个 struct ),它不会报告其超类的属性,即:

    class Person : PropertyNames {
        var name = "Bruce Wayne"
    }
    
    class Superhero : Person {
        var hasSuperpowers = true
    }
    
    Superhero().propertyNames() // ["hasSuperpowers"] — no "name"
    

    根据你想要的行为,你可以使用superclassMirror()来解决这个问题.

Using class_copyPropertyList

如果您使用的是Objective-C对象,您可以使用以下方法:

var count = UInt32()
let classToInspect = NSURL.self
let properties : UnsafeMutablePointer <objc_property_t> = class_copyPropertyList(classToInspect, &count)
var propertyNames = [String]()
let intCount = Int(count)
for var i = 0; i < intCount; i++ {
    let property : objc_property_t = properties[i]
    guard let propertyName = NSString(UTF8String: property_getName(property)) as? String else {
        debugPrint("Couldn't unwrap property name for \(property)")
        break
    }

    propertyNames.append(propertyName)
}

free(properties)
print(propertyNames)

如果classToInspectNSURL,则输出至控制台:

["pathComponents", "lastPathComponent", "pathExtension", "URLByDeletingLastPathComponent", "URLByDeletingPathExtension", "URLByStandardizingPath", "URLByResolvingSymlinksInPath", "dataRepresentation", "absoluteString", "relativeString", "baseURL", "absoluteURL", "scheme", "resourceSpecifier", "host", "port", "user", "password", "path", "fragment", "parameterString", "query", "relativePath", "hasDirectoryPath", "fileSystemRepresentation", "fileURL", "standardizedURL", "filePathURL"]

这在操场上是行不通的.只需将NSURL替换为EachDayCell(或者重复使用与扩展相同的逻辑),就可以了.

Swift相关问答推荐

SwiftData:线程1:&Quot;NSFetchRequest找不到实体名称';提醒&q;的NSEntityDescription

当计数大于索引时,索引超出范围崩溃

macOS SwiftUI: 如何触发删除一个项目?

解码 JSON 时 Swift Struct Decoder 初始化程序错误

ForEach within List:模式的好处?

TextField 键入未完成

如何在 UITableView 中点击图片和标题

按钮图像不会立即刷新

响应 UITextField (UIViewRepresentable) 中的特定按键

符合协议 - 错误?

如何在填充上添加 if else 语句? SwiftUI

是否可以使任何协议符合 Codable?

Swift初始化具有可变ID的重复值数组

如何在 Swift 中的两个 UIView 中心之间添加线?

如何让不同的组件拥有不同的动画?

如何删除标签和步进按钮之间的间距是 SwiftUI Stepper?

RealityKit – 无法加载 ARView(发现为零)

如果没有标记,则为 Swift 预处理器

Swift :如何在一组字符之后得到所有东西

如何快速识别字符串中的大写和小写字符?