Skip to main content
 首页 » 操作系统

swift之swift 中的可变复制

2024年01月01日38bonelee

有可能获得像 (obj-c) 这样的可变副本

NSMutableArray *lists = [self.fetchedResultsController.fetchedObjects mutableCopy]; 

swift ?

我正在尝试重新安排一些核心数据实体

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) 

有什么想法吗?

请您参考如下方法:

如果数据是值类型(结构、数组、字典、字符串等),只需将不可变变量分配给可变变量即可:

let immutable = [1, 2, 3] 
var mutable = immutable 
mutable[0] = 55 // mutable now is [55, 2, 3], whereas immutable is of course not modified 

如上所述,这仅适用于值类型,因为赋值是按值而不是按引用完成的。