我正試圖傳遞資料,得到了這個錯誤。'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead. Here is my code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CustomLoopsCell = beatTableView.dequeueReusableCell(withIdentifier: "firstLoopCell", for: indexPath) as! CustomLoopsCell
let beatLoop = overtunePacks[indexPath.row] 。
//>在下降的行中,我得到這個錯誤。
let beatDesc = loopsDesc[indexPath.row] 。
let beatProd = loopsProd[indexPath.row]
let beatAudio = loopsAudio[indexPath.row]
let beatInst = loopsInst[indexPath.row]
cell.loopNameLabel.text = beatDesc
cell.producerLabel.text = beatProd
cell.instrumentLabel.text = beatInst
下面是另一個視圖控制器的代碼,我在那里傳遞資料:
我在那里傳遞資料。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?. instantiateViewController(identifier: "BeatViewID") as? BeatPackViewController。
vc?.beatInfoCollectName = beatInfoCollect[indexPath.row].name
vc?.beatProducerName = beatInfoCollect[indexPath.row] .producer
vc?.imageBeat = beatInfoCollect[indexPath.row].imagename
vc?.loopsAudio = beatAudioCollect[indexPath.row].loopPath
vc?.loopsDesc = beatAudioCollect[indexPath.row].name
vc?.loopsInst = beatAudioCollect[indexPath.row].instrument
vc?.loopsProd = beatAudioCollect[indexPath.row] .producer
self.navigationController?.pushViewController(vc! , animated: true)
}
這是我在第二個視圖控制器中的變數,我在這里傳遞資料:
這是我的變數。
var loopsAudio = ""
var loopsDesc = ""/span>
var loopsInst = ""/span>
var loopsProd = ""
這是我想顯示的內容:
struct BeatLoops。Decodable {
let name: String[/span]。
let instrument: String
let loopPath: String
let producer: String
var songURL : URL {
let components = loopPath.component(separateBy: "."/span>)
guard components.count == 2,
let url = Bundle.main.url(forResource: components[0], withExtension: components[1] ) else { fatalError("音頻檔案丟失") }
return url
}
}
我正在決議這個資料,有實體。var beatAudioCollect = [BeatLoops]()/code>
uj5u.com熱心網友回復:
看來你是想從loopsDesc字串中獲取Substring
let beatDesc = loopsDesc[indexPath.row]
...
let beatInst = loopsInst[indexPath.row] 。
重點是你不能只用訂閱索引來獲得String的字符。
如果你想從一個字串中獲得一個字符,你應該在指定的范圍內獲得它的子串。
就像這樣:
let index = loopsDesc.index(loopsDesc.startIndex, offsetBy: 1)
let mySubstring = loopsDesc[.<index]
或者另一種方法:
1--將這個擴展名添加到一個字串中:
extension StringProtocol{
subscript(offset: Int) -> character{
self[index(startIndex, offsetBy: offset)] 。
}
}
2 - 像這樣使用它
let input = "My String Here"
let char = input[3]
/or[/span>]
let char = String(input[3] )
請看另一個堆疊溢位問題與此相關。
但這是不安全的方法,我建議使用另一種方法來獲取引數(例如將它們存盤在陣列中,而不是字串中)
uj5u.com熱心網友回復:
你的設計是錯誤的。
在表視圖中從一個字串中提取單個字符并在不同的行中顯示是沒有意義的。
事實上,你正在向第二個視圖控制器傳遞兩個物件,一個infoCollect物件和一個audioCollect物件。
首先,不要提取所有的屬性,而只是傳遞整個物件。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?. instantiateViewController(identifier: "BeatViewID") as! BeatPackViewController
vc.infoCollect = beatInfoCollect[indexPath.row] 。
vc.audioCollect = beatAudioCollect[indexPath.row]。
self.navigationController?.pushViewController(vc, animated: true)
}
順便說一下,強烈不建議使用多個陣列作為資料源!
在第二個控制器中,將
String屬性替換為
var infoCollect : Loop!
var audioCollect : BeatLoops!
如果結構是在第一個視圖控制器中宣告的,你必須寫
var infoCollect : BPLibraryViewController.Loop !
var audioCollect : BPLibraryViewController.BeatLoops!
然后洗掉表視圖并添加UILabels來代替。在viewDidLoad中分配值,例如
imageBeatLabel.text = infoCollect.name
loopsAudioLabel.text = audioCollect.loopPath
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/311700.html
標籤:
