2017-09-21 89 views
-1

我試圖創建一個空的全局字符串arrray斯威夫特全球陣列

struct GlobalVariables { 
    static var globalString = [String]() 
} 

我想通過將字符串值放入數組

func percentageCalculation() -> String { 
     var FinalString = String() 

     for i in 1...100 { 
      let tstring = String("\(i)%\n") 
      GlobalVariables.globalString[i] = tstring 
     } 

     return FinalString 
    } 

然後outputing初始化它像這樣全局字符串值到我的tableview。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

     let testLabel = cell?.viewWithTag(1) as! UITextView 

     testLabel.text = GlobalVariables.globalString[indexPath.row] 

     return cell! 
    } 

有關最佳實現方法的建議?這是我試圖完成的僞代碼

回答

1

你不需要一個結構來定義一個簡單的數組。

var globalString = [String]() 

一旦你的數組,你可以添加你的號碼給它這樣的:

for i in 1...100 { 

    globalString.append("\(i)") 
} 

然後在你的cellForRow你可以用你只可以在全球範圍內像這樣定義一個數組這填充值:

testLabel.text = globalString[indexPath.row]