gpt4 book ai didi

arrays - swift 2.0 - 错误 : Immutable value of type '[String]' only has mutating members named 'append'

转载 作者:行者123 更新时间:2023-11-28 13:02:24 24 4
gpt4 key购买 nike

我正在尝试编写一个 methodHTML tag 提取到数组中,因为我想在树中解析网页结构。

  func extractStringFromHTMLInsideTags(htmlString:String, htmlTagPairArray:[String], saveToArray:[String]) -> String
{
var htmlNSString = htmlString as NSString
var lenght = htmlNSString.length

var openingTag = htmlTagPairArray[0] as NSString
var openingTagLength = openingTag.length

var closingTag = htmlTagPairArray[1] as NSString
var closingTagLength = closingTag.length

if (htmlString.rangeOfString(htmlTagPairArray[0]) != nil)
{
let rangeStart:NSRange! = htmlNSString.rangeOfString(htmlTagPairArray[0], options: NSStringCompareOptions.CaseInsensitiveSearch)
var rangeEnd:NSRange! = htmlNSString.rangeOfString(htmlTagPairArray[1], options: NSStringCompareOptions.CaseInsensitiveSearch)

let startTagIndex: Int = rangeStart.location + openingTagLength
let boldTextLenght: Int = rangeEnd.location - rangeStart.location - openingTagLength
let endTagIndex: Int = startTagIndex + boldTextLenght


let startIndex = advance(htmlString.startIndex,startTagIndex)//advance as much as you like
let endIndex = advance(htmlString.startIndex,endTagIndex)
let range = startIndex..<endIndex
var resultString = htmlString.substringWithRange( range )

saveToArray.append(resultString)

resultString = htmlString.stringByReplacingOccurrencesOfString(htmlTagPairArray[0] + resultString + htmlTagPairArray[1], withString: resultString, options: nil, range: nil)

if (resultString.rangeOfString(htmlTagPairArray[0]) != nil)
{
resultString = extractStringFromHTMLInsideTags(resultString,htmlTagPairArray:htmlTagPairArray, saveToArray:saveToArray)
}

return resultString

}

return htmlString
}

线上:

saveToArray.append(resultString)

我收到错误:

Immutable value of type '[String]' only has mutating members named 'append'

错误指出 saveToArray 是不可变的。我认为数组是通过引用复制的...?

为什么会这样?

(我使用的是 Swift 2.0 和 Xcode 7.1)。

最佳答案

方法中传入的参数默认是不可变的(let)。
添加关键字 var 使它们可变

func extractStringFromHTMLInsideTags(htmlString:String, htmlTagPairArray:[String], var saveToArray:[String]) -> String
{ ...

关于arrays - swift 2.0 - 错误 : Immutable value of type '[String]' only has mutating members named 'append' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33570186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com