2017-09-21 87 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]