Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 迭代两个自定义数组,如果变量相等,则设置值_Ios_Arrays_Swift_Foreach_Iteration - Fatal编程技术网

Ios 迭代两个自定义数组,如果变量相等,则设置值

Ios 迭代两个自定义数组,如果变量相等,则设置值,ios,arrays,swift,foreach,iteration,Ios,Arrays,Swift,Foreach,Iteration,我有一个简单的问题,但同时也很难回答。我有两个独立的结构(这也适用于类): 及 我有一个结构数组var fbTweetArray:[FBTweet]=[]和var statusArray:[Status]=[] 我已将中的每个变量设置为fbTweetArray中每个索引中的某个值,但我仅在statusArray的每个索引中设置了.statusId变量。对于statusArray中的每个statusArray.statusId值,只有一个fbTweetArray.tweetId具有相同的精确Int

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

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

我已将中的每个变量设置为fbTweetArray中每个索引中的某个值,但我仅在statusArray的每个索引中设置了
.statusId
变量。对于statusArray中的每个
statusArray.statusId
值,只有一个
fbTweetArray.tweetId
具有相同的精确Int值。我想做的是,如果这两个变量相同,那么我应该设置
statusArray.statusText
到fbTweetarray.tweetText所属的任何对象。例如,只有
fbTweetArray[1]。tweetid=2346
statusArray[4]。statusId=2346
的值为2346。如果fbTweetArray[1].tweetText=“hello friend”那么[code>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循环中将var status的
设置回statusArray中,因为它现在是一个var,并且与
var statusArray:[status]=[]
中的一个索引不同。只有当两个数组都没有排序时,您的问题才有意义

要从fbTweet数组中查找元素,可以对其进行排序并使用二进制搜索。 然后枚举状态数组,找到具有相同标识符的fbTweet对象并修改状态对象。在写入时复制结构时,需要将其再次保存在数组中

extension Array where Element == FBTweet {
    func binarySearchFBTweetWith(_ id:Int) -> FBTweet? {
        var range = 0..<self.count
        while range.startIndex < range.endIndex {
            let midIndex = range.startIndex + (range.endIndex - range.startIndex) / 2
            guard let tweetId = self[midIndex].tweetId else {
                continue
            }
            if tweetId == id {
                return self[midIndex]
            } else if tweetId < id {
                range = midIndex+1..<range.endIndex
            } else {
                range = range.startIndex..<midIndex
            }
        }
        return nil
    }
}

fbTweetArray.sort{($0.tweetId ?? 0) < ($1.tweetId ?? 0)}
for (index, status) in statusArray.enumerated() {
    guard let statusId = status.statusId else {continue}
    guard let fbTweet = fbTweetArray.binarySearchFBTweetWith(statusId) else {continue}
    var status = status
    status.statusText = fbTweet.tweetText
    statusArray[index] = status
}
扩展数组,其中元素==FBTweet{
func binarySearchFBTweetWith(id:Int)->FBTweet{

var range=0..只有当两个数组都没有排序时,您的问题才有趣

要从fbTweet数组中查找元素,可以对其进行排序并使用二进制搜索。 然后枚举状态数组,找到具有相同标识符的fbTweet对象并修改状态对象。在写入时复制结构时,需要将其再次保存在数组中

extension Array where Element == FBTweet {
    func binarySearchFBTweetWith(_ id:Int) -> FBTweet? {
        var range = 0..<self.count
        while range.startIndex < range.endIndex {
            let midIndex = range.startIndex + (range.endIndex - range.startIndex) / 2
            guard let tweetId = self[midIndex].tweetId else {
                continue
            }
            if tweetId == id {
                return self[midIndex]
            } else if tweetId < id {
                range = midIndex+1..<range.endIndex
            } else {
                range = range.startIndex..<midIndex
            }
        }
        return nil
    }
}

fbTweetArray.sort{($0.tweetId ?? 0) < ($1.tweetId ?? 0)}
for (index, status) in statusArray.enumerated() {
    guard let statusId = status.statusId else {continue}
    guard let fbTweet = fbTweetArray.binarySearchFBTweetWith(statusId) else {continue}
    var status = status
    status.statusText = fbTweet.tweetText
    statusArray[index] = status
}
扩展数组,其中元素==FBTweet{
func binarySearchFBTweetWith(id:Int)->FBTweet{

var range=0..基本上,您只需要一个
来实现
/
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
s都可以是
nil
。要处理这种情况(如果两个id都为nil-对象不相等),可以编写自定义
=
函数:

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
协议,您将能够将
fbteet
s和
Status
es放入
Set
结构中。其好处是:

如果将这些对象存储在一个集合中,理论上可以 在恒定时间(O(1))中查找其中任何一个-即,在 设置为10个元素所需的时间与在 设置为10000


你可以在一个新的great中找到更多关于它的深入信息。

基本上,你只需要一个
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
s都可以是
nil
。要处理这种情况(如果两个id都为nil-对象不相等),可以编写自定义
=
函数:

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
协议,您将能够将
fbteet
s和
Status
es放入
Set
结构中。其好处是:

如果将这些对象存储在一个集合中,理论上可以 在恒定时间(O(1))中查找其中任何一个-即,在 设置为10个元素所需的时间与在 设置为10000


您可以在一个新的great中找到关于它的更深入的信息。

另一种选择是使用字典而不是数组,以获得更好的性能和更容易的实现(在本例中)。如果需要,您可以稍后轻松地获得键和值数组


在这种情况下,Id将是字典的键,文本将是值。

另一种选择是使用字典而不是数组,以获得更好的性能和更容易的实现(在这种情况下)。如果需要,您可以稍后轻松获取键和值的数组


在这种情况下,Id是字典的键,文本是值。

“迭代两个数组,比较.tweetId和.statusId值,然后正确设置文本变量”这是答案?还是您正在寻找一个更简单的答案?在Swift,code-wise@RakeshaShastri中,我到底如何做到这一点“迭代两个数组,比较.tweetId和.statusId值,然后正确设置文本变量”这就是答案?还是你在寻找一个更简单的答案?在Swift,code-wise@RakeshaShastri中,我该怎么做