ios - 遍历两个自定义数组并在变量相等时设置值 Swift

标签 ios arrays swift foreach iteration

我有一个简单的问题同时也很难。我有两个独立的结构(这也适用于类):

struct FBTweet {
    var tweetId: Int? //set
    var tweetText: String?  //set
}

struct Status {
    var statusId: Int? //set
    var statusText: String? //no value
    }

我有一个结构数组 var fbTweetArray: [FBTweet] = []var statusArray: [Status] = []

我已经在 fbTweetArray 的每个索引中将每个变量设置为特定值,但我只在 statusArray 的每个索引中设置了 .statusId 变量。对于 statusArray 中的每个 statusArray.statusId 值,只有一个 fbTweetArray.tweetId 具有完全相同的 Int 值。我想做的是,如果这两个变量相同,那么我应该设置 statusArray.statusText 到任何 fbTweetarray.tweetText 是。例如,只有 fbTweetArray[1].tweetid = 2346statusArray[4].statusId = 2346 的值是 2346。如果 fbTweetArray[1].tweetText = "hello friend" 那么 statusArray[4].statusText 需要设置为 "hello friend"。

到目前为止我有

func testWhat () {

    var fbTweetArray: [FBTweet] = []
    var statusArray: [Status] = []

    for  fbTweet in fbTweetArray {
        for var status in statusArray {
            if (status.statusId == fbTweet.tweetId ) {
                status.statusText = fbTweet.tweetText
            }
        }
    }
}

我如何将 for 循环中的 for var status 设置回 statusArray,因为它现在是一个 var 并且不同于 var statusArray 中的索引之一:[Status] = []

最佳答案

基本上,你只需要一个for/forEach循环来实现你想要的:

var fbTweetArray: [FBTweet] = [
    FBTweet(tweetId: 1, tweetText: "1"),
    FBTweet(tweetId: 2, tweetText: "2"),
    FBTweet(tweetId: 3, tweetText: "3")
]

var statusArray: [Status] = [
    Status(statusId: 2, statusText: nil),
    Status(statusId: 1, statusText: nil),
    Status(statusId: 3, statusText: nil)
]

fbTweetArray.forEach { tweet in
    if let index = statusArray.index(where: { $0.statusId == tweet.tweetId }) {
        statusArray[index].statusText = tweet.tweetText
    }
}

print(statusArray.map { $0.statusText }) // [Optional("2"), Optional("1"), Optional("3")]

请注意,您在两个结构中的 id 都可以是 nil。要处理这种情况(如果两个 id 都为 nil - 对象不相等),您可以编写自定义 == func:

struct Status {
    var statusId: Int? //set
    var statusText: String? //no value

    static func == (lhs: Status, rhs: FBTweet) -> Bool {
        guard let lhsId = lhs.statusId, let rhsId = rhs.tweetId else { return false }
        return lhsId == rhsId
    }
}

...

// rewrite .index(where: ) in if condition
if let index = statusArray.index(where: { $0 == tweet }) { ... }

此外,还有一些专业提示。如果您将结构采用 Hashable 协议(protocol),您将能够将 FBTweetStatus 放入 Set结构。这样做的好处:

If you instead store those objects in a set, you can theoretically find any one of them in constant time (O(1)) — that is, a lookup on a set with 10 elements takes the same amount of time as a lookup on a set with 10,000.

您可以在新的 article by NSHipster 中找到更多关于它的深入信息.

关于ios - 遍历两个自定义数组并在变量相等时设置值 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51889087/

相关文章:

c++ - 修改 C++ 字符串对象的底层 char 数组

ios - 在 Swift 中实现 SWRevealViewController

ios - 普遍限制 UITextfield 输入

ios - Tableview标题单元格在swift中使用相同的cardView扩展?

ios - 通过 iPhone 设备扫描三个具有相同 CBUUID 但 MAC 地址不同的相同 BLE 传感器

ios - SKMutablePayment 属性 : applicationUsername

arrays - 从数组创建字符串的函数 (Mathematica)

ios - 在 Swift 中的 Apple map 上添加标记标签

ios - 我们可以通过编程方式访问通用/辅助功能/字幕和字幕的 iOS 系统设置吗?

c - 为什么可变长度数组在此 C 代码中工作?