class BaseViewController: UIViewController,departmentMenuDelegate{
var deptId1: NSMutableArray = []
var deptSelectedIndexPath : NSIndexPath?
var deptSelectedProfileName : String = ""

func returnDeptId()-> NSMutableArray
{
    return deptId1
}
}

class DepartmentMenuController: UIViewController,,UITableViewDataSource, UITableViewDelegate{
var deptId: NSMutableArray = []

var testing : Int = 0
weak var delegate : departmentMenuDelegate?

override func viewDidLoad() {
    super.viewDidLoad()
    deptId = (delegate?.returnDeptId())!
    deptId = ["suds","sdsds"]
}
}


问题1:当我在DepartmentMenuController中将值添加到deptId时,它将自动将值存储到BaseViewController中的deptId。为什么会发生?如果我删除deptId =(delegate?.returnDeptId())!,则该问题不存在

NSMutableArray只会发生这种情况,而其他类型则不会发生这种情况(例如String)

问题2:在正常情况下,会发生这种情况吗?我想知道这是我程序中的问题吗

最佳答案

当您致电代表时,您正在通过引用传递。我认为这就是您想要的。

class BaseViewController: UIViewController,departmentMenuDelegate{
  var deptId: NSMutableArray = []
  var deptSelectedIndexPath : NSIndexPath?
  var deptSelectedProfileName : String = ""

  func returnDeptId(departmentID: NSMutableArray) -> NSMutableArray {
      for i in 0..<deptId.count {
        departmentID.append(deptId[i] copy)
      }
      return departmentID
  }
}

class DepartmentMenuController: UIViewController, UITableViewDelegate {

  var deptId: NSMutableArray = []
  var testing : Int = 0
  weak var delegate : departmentMenuDelegate?

  override func viewDidLoad() {
      super.viewDidLoad()
      deptId = (delegate?.returnDeptId(deptId))!
      deptId = ["suds","sdsds"]
  }
}

关于ios - 通过委派自动存储数据(快速),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38113451/

10-10 00:57