超过最大Text(“")串联长度-SwiftUI- [英] Exceeding max Text("") concatenation length - SwiftUI -

查看:110
本文介绍了超过最大Text(“")串联长度-SwiftUI-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考Asperi发布的答案( https://stackoverflow.com/users/12299030/asperi )问题:在SwiftUI中突出显示文本的特定部分

With reference to the answer posted by Asperi (https://stackoverflow.com/users/12299030/asperi) on Question: Highlight a specific part of the text in SwiftUI

我发现他的回答非常有用,但是,当我的String输入超过32k个字符时,应用崩溃了,因此我假设String()的最大值为32k,并且正在寻找解决方法.

I have found his answer quite useful, however, when my String input exceeds 32k characters the app crashes, so I am assuming the String() is a max of 32k and am looking for a work around.

在我的应用程序中,如果有人搜索单词"pancake",则将存储该搜索词,并且当用户查看(可以说是食谱的)详细信息页面时,单词pancake将突出显示.所有人都可以很好地解决这个问题,但是当配方超过32k个字符时,应用会因超出索引范围的消息而崩溃. (特定的错误消息:线程1:EXC_BAD_ACCESS(代码= 2,地址= 0x16d43ffb4))

In my app, if someone searches for the word "pancake", the search word will be stored and when the user looks at the detail page (of lets say a recipe), the word pancake will highlight. All works well with this answer, but when the recipe exceeds 32k characters, the app crashes with exceeding index range messages. (specific error message: Thread 1: EXC_BAD_ACCESS (code=2, address=0x16d43ffb4))

这是该问题答案的修改后的代码:

Here is the modified code from the answer on that question:

这将打印数据:

hilightedText(str: self.recipes.last!.recipeData!)
                        .multilineTextAlignment(.leading)
                        .font(.system(size: CGFloat( settings.fontSize )))

上面的代码显然还有更多内容,但是从本质上讲,它会迭代数据库,并找到包含搜索词"的最后一条记录,并在此处显示配方数据,这是数据库中包含的大字符串.

There is obviously more to this code above, but in essence, it iterates a database, and finds the last record containing 'search word' and displays the recipeData here, which is a large string contained in the database.

以实现HighlightedText功能:

to implement the highlightedText functionality:

    func hilightedText(str: String) -> Text {
        let textToSearch = searched
        var result: Text!
        for word in str.split(separator: " ") {
            var text = Text(word)
            if word.uppercased().contains(textToSearch.uppercased()) {
                text = text.bold().foregroundColor(.yellow)
            }
            //THIS NEXT LINE has been identified as the problem:
            result = (result == nil ? text : result + Text(" ") + text)
        }
        return result
    }

我已经稍微修改了Asperi的答案以适应我的需要,并且一切工作都非常好,除非我遇到过一个大小大于32k的配方数据条目,如前所述.

I've modified the answer from Asperi slightly to suit my needs and all works really well, unless I come across a recipeData entry that is larger than 32k in size, as stated before.

我尝试用其他几种数据类型替换String,但是没有任何效果.

I have tried Replacing String with a few other data types and nothing works..

有什么想法吗?

谢谢!

更新:

在评论中进行了长时间讨论之后,看来问题的根本原因是在某些时候,对于某些记录,我超出了最大的Text(")串联.

After lengthy discussion in the comments, it appears that the root cause of the issue is at some point, for some records, I am exceeding the maximum Text("") concatenations.

在上面的代码中,每个单词都被分离,评估并添加到长字符串"result"中.最终看起来像这样: Text("word") + Text(" ") + Text("Word") 等等.

In the above code, each word is split out, evaluated and added to the long string "result" which winds up looking like this: Text("word") + Text(" ") + Text("Word") and so on.

完成了此操作,因此我可以轻松地为每个单词应用颜色属性,但是似乎一旦我打了一定数量的单词(少于32k,一条记录为22k并崩溃),应用就会崩溃.

This is done, so I can easily apply color attributes per word, but it would seem that once I hit a certain number of words (which is less that 32k, one record was 22k and crashed), the app crashes.

Leo建议 https://stackoverflow.com/a/59531265/2303865 此线程可以替代,我将必须尝试实现它.

Leo suggested https://stackoverflow.com/a/59531265/2303865 this thread as an alternative and I will have to attempt to implement that instead.

谢谢..

推荐答案

嗯...出乎意料的局限...无论如何-学习一些新知识.

Hmm... unexpected limitation... anyway - learn something new.

好的,这是一种改进的算法,应该将限制移到很远的地方.

Ok, here is improved algorithm, which should move that limitation far away.

在Xcode 12/iOS 14上进行了测试.(还更新了参考主题 SwiftUI中文本的特定部分)

Tested with Xcode 12 / iOS 14. (also updated code in referenced topic Highlight a specific part of the text in SwiftUI)

func hilightedText(str: String, searched: String) -> Text {
    guard !str.isEmpty && !searched.isEmpty else { return Text(str) }

    var result = Text("")

    var range = str.startIndex..<str.endIndex
    repeat {
        guard let found = str.range(of: searched, options: .caseInsensitive, range: range, locale: nil) else {
            result = result + Text(str[range])
            break
        }

        let prefix = str[range.lowerBound..<found.lowerBound]
        result = result + Text(prefix) + Text(str[found]).bold().foregroundColor(.yellow)

        range = found.upperBound..<str.endIndex
    } while (true)

    return result
}

这篇关于超过最大Text(“")串联长度-SwiftUI-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆