试图将文本附加到Swift中的循环内的属性中

时间:2017-08-18 19:20:53

标签: ios swift struct

我有一个struct对象数组。这些对象具有title属性(String),以及location(属性Location)的属性。

我想要的是将location属性和距离函数与另一个现有Location对象派生的距离的double值附加到title属性。这是我正在使用的代码:

self.myList.map({$0.title.append("\($0.location.distance(from: location!)/1000)")})

这里的问题是map函数返回一个新数组,但是,我需要对现有数组进行更改,因为我使用这个数组作为我的UITableView的数据源。另一个问题是,尽管我将title属性设为var,但我总是被告知以下错误消息:

Cannot use mutating member on immutable value: '$0' is immutable.

任何人都可以弄清楚如何做到这一点?

2 个答案:

答案 0 :(得分:1)

你应该像这样迭代数组:

.bat

答案 1 :(得分:0)

您必须使用临时可变变量:

string Word = "asshole";

if (Comment.Contains(Word)
    //block the comment from being posted