ios - cocoa 指数

标签 ios swift macos cocoa

我习惯了制作 iPhone 应用程序,但现在我需要制作 Mac 应用程序。 因此我必须切换到 Cocoa 框架。

有没有类似于 Cocoa 中的 array.index(of: ) 的东西?

iOS 示例:

let string = "Hello.World"
let needle: Character = "."
if let idx = string.characters.index(of: needle) {
    let pos = string.characters.distance(from: string.startIndex, to: idx)
    print("Found \(needle) at position \(pos)")
}
else {
    print("Not found")
}

我遇到问题的实际代码在这里:

func checkBoxTapped(_ sender: NSButton) {
    if (sender.state == 1) {
        self.selectedReferrals.append(self.referralApplicationList[sender.tag])
    }
    if (sender.state == 0) {
        // Remove
        // self.referralApplicationList[sender.tag]
        // let foofoo = self.referralApplicationList.indexOf(self.referralApplicationList[sender.tag])
    }
}

最佳答案

您的问题与 Cocoa 或 macOS 与 iOS 完全无关。您真正要问的是如何获得 index(of:) Swift 数组上的函数,答案是数组中的元素需要是 Equatable

在您的 iOS 示例中,charactersString.CharacterView 的一个实例其中明确有 index(of:)功能。

在您的 Mac 示例中,您没有提供足够的信息,selectedReferrals变量大概是 Array一些Referral类型;例如[Referral] 。因为有类似的index(of:)你的Referral类型需要实现Equatable协议(protocol)。否则你需要使用类似 index(where:) 的东西找到您想要的。

关于ios - cocoa 指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47451176/

相关文章:

ios - 如何在 Swift 中创建自定义 UITableViewCell 类?

ios - 将 subview 添加到自定义 UI 按钮类

Java OS X - 扩展坞中没有应用程序图标

python - 凌乱的 Python 安装? (OS X)

ios - UI 导航栏标题和右栏按钮显示在我拥有的所有 View Controller 中

ios - 如何在 iOS (Swift) 上创建后台进程?

ios - 在 Swift 中保存 CoreData 对多关系

string - 如何在 swift 3.0 中连接多个可选字符串?

macos - RabbitMQ Go 教程日志.Printf 无法写入磁盘文件?

iOS - Accelerate.framework 中矩阵的逐元素条件边界