我有一个字符串,我需要在其中提取前3个数字,并在计算完后再提取3个数字。
代码如下:
let val = pref.string(forKey: "RemoteData")
print(val) //Optional("23,8,21,1,2,16,17,18,11,23,14,6,8,13,4,21,15,22,1,2,9,16,17,7,18,11,32,14,6,33,23,8,31,21,1,3,2,16,24,17,18,5")
我需要做这样的事情:
let startCount = pref.object(forKey: "startCountValue") as? Int ?? 0
let lstCount = pref.object(forKey: "LastCountValue") as? Int ?? 3
let UnlockId = arrayV[startCount..<lstCount]
我在上面对整数数组进行过此操作。但是在这里,我不知道如何从字符串中获取前三个值:
23,8,21
我需要这样做let UnlockId = arrayV[startCount..<lstCount]
。
一段时间后,我会将startCount
增加到+ 3,因此下一次从4增加到6。
问题出在Optional("23,8,21,1,2,16,17,18,11,23,14,6,8,13,4,21,15,22,1,2,9,16,17,7,18,11,32,14,6,33,23,8,31,21,1,3,2,16,24,17,18,5")
我喜欢从0到3的第一个3的方式。我喜欢let UnlockId = arrayV[startCount..<lstCount]
。但是在这种情况下,我该如何对我的上述值做同样的事情?
更新的代码:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let gameId = gameInfo["game_id"] as? String // its printing correct values
let val = pref.string(forKey: "RemoteData")!
let arr = val.components(separatedBy:",")
let startCount = pref.object(forKey: "startCountValue") as? Int ?? 0
let lstCount = pref.object(forKey: "LastCountValue") as? Int ?? 3
let unlockId = arr[startCount..<lstCount]
if unlockId.contains(gameId!) {
print("its locked")
} else {
print("its not locked")
}
}
在此处崩溃:unlockId.contains(gameId!)
,但游戏ID未打印。变成零。
答案 0 :(得分:1)
您可以尝试
Sub WriteXMLContents2Sheet()
Const MAXCOLUMNS& = 15 ' << change to columns limit in xml
Dim linEscrita As Long: linEscrita = 2 ' << start row
Application.ScreenUpdating = False
Dim wsBase As Worksheet
Set wsBase = ThisWorkbook.Worksheets("Name of worksheet")
'[1] load xml
Dim resp As MSXML2.DOMDocument60
Dim lista As IXMLDOMNodeList
Dim nodeAtual As IXMLDOMNode
Dim childNode As IXMLDOMNode
Set resp = New DOMDocument60
resp.LoadXML (FunctionForGettingXMLfromWebService)
'[2] set nodelist to memory (XPath possibly could be refined :-)
Set lista = resp.SelectNodes("//node1/node2")
'[3] dimension temporary variant (1-based) 2-dim array to hold contents
ReDim tmp(1 To lista.Length, 1 To MAXCOLUMNS)
' [4] loop thru nodelist
Dim c&, r& ' declare row/column counters
r = 1
For Each nodeAtual In lista
c = 1
If nodeAtual.HasChildNodes Then
For Each childNode In nodeAtual.ChildNodes
tmp(r, c) = childNode.Text
c = c + 1 ' increment column counter
Next childNode
r = r + 1 ' << move row counter into If condition to avoid empty lines
End If
Next nodeAtual
'[5] write array content to sheet
wsBase.Range("A" & linEscrita).Resize(UBound(tmp), UBound(tmp, 2)) = tmp
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:0)
尝试一下:
func testeFunc(arrayItens: String) {
let stringArray = arrayItens.components(separatedBy: ",")
var arrayTemp = [String]()
var count = 0
for item in stringArray {
if count <= 3 {
arrayTemp.append(item)
count += 1
}else{
print(arrayTemp)
arrayTemp.removeAll()
count = 0
}
}
}