更新/更改自定义对象的数组值-Swift

时间:2019-10-06 14:07:59

标签: arrays swift

我有一个自定义的model,如下:

struct ContentModel {
    var id: Int64?
    var parentID: Int64?
    var subject: String?
    var langId: String?
    var isSelected: String?
    var identifier: String?
    var title: String?
    var count: Int64?
    var selectAll: String?
    var textSelectAll: String?
}

并且:

struct SectionModel {
    var expand:Bool
    var title:String
    var content:[ContentModel]
}

如何从上述模型中更新特定项目?

我正在使用以下代码:

for resRoot in  displayListCourseFilter{
    for resSub in resRoot.content{
        if pId == resSub.parentID{
            resSub.selectAll = "true"
            resSub.isSelected = "true"
        }
    }
}

但是说我

Cannot assign to property: 'resSub' is a 'let' constant

注意:我所有的变量都是var

1 个答案:

答案 0 :(得分:1)

使用结构

public int leafSimilar(TreeNode root, int arr[], int l) {

    if(root == null)
    {
        return l;
    }

    if(root.left == null && root.right == null)
    {
        arr[l] =root.val ;
        l+=1;
        return l;
    }        
    l = leafSimilar(root.left, l);
    l = leafSimilar(root.right, l);
    return l;
}

public boolean compareLeaves(int arr1[], int arr2[], int l, int r)
 {
     if( l != r ) return false;

     for(int i = 0; i <l ;i ++)
     {
       boolean flag = true;
        for(int j = 0; j <r ;j ++) {
         if(arr1[i] == arr2[j])
         {
            flag = false;
            break;
         }
       }
       if( flag) return false;
    }
     return true;
 }

int l = leafSimilar(root1, arr1, 0);
int r = leafSimilar(root2, arr2, 0);
compareLeaves(arr1, arr2, l, r);

或使其成为一个类,它将进行编译