Tag: 不变性

“不能分配给属性:”任何“是不可变的”错误

tiledViewsStack包含UIImageViews。 我试图给数组中的每个UIImageView一个新的中心坐标。 不知道为什么我得到这个错误的for循环的最后一行… var tiledViewsStack = [AnyObject]() var randLocInt = Int() var randLoc = CGPoint() for var any in tiledViewsStack { randLocInt = Int((arc4random()*10) % 9) // 0, — 8 randLoc = allCenters[randLocInt].CGPointValue() any.center = randLoc }

是否有可能创build从plist读入的对象的可变版本

我正在阅读我的应用程序plist,在顶层是一个数组。 确保数组开始为可变是很简单的 self.plistData = [[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myDataSource" ofType:@"plist"]] mutableCopy]; 数组中的每个元素都是另一个数组,每个数组都包含一个字典。 基于表格单元格select,我正在改变选定索引处字典的属性: [self.cellDescriptors[indexPath.section][indexOfTappedRow] setValue:@"YES" forKey: @"isSelected"]; 我收到错误'-[__NSCFDictionary removeObjectForKey:]: mutating method sent to immutable object'当我尝试更改字典值时, '-[__NSCFDictionary removeObjectForKey:]: mutating method sent to immutable object' 。 如果由plist读入的NSDictionary是不可变的,那么错误是有意义的。 有没有办法从plist中读取内容,并确保任何数组或字典被读作可变版本?

Swift中的不可变/可变集合

我指的是Apple Swift编程指南,用于理解Swift语言中可变/不可变对象(数组,字典,集合,数据)的创build。 但是我不明白如何在Swift中创build一个不可变集合。 我想在Objective-C中的Swift中看到下面的等价物 不可变arrays NSArray *imArray = [[NSArray alloc]initWithObjects:@"First",@"Second",@"Third",nil]; 可变数组 NSMutableArray *mArray = [[NSMutableArray alloc]initWithObjects:@"First",@"Second",@"Third",nil]; [mArray addObject:@"Fourth"]; 不可变的词典 NSDictionary *imDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Value1", @"Key1", @"Value2", @"Key2", nil]; 可变的字典 NSMutableDictionary *mDictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Value1", @"Key1", @"Value2", @"Key2", nil]; [mDictionary setObject:@"Value3" forKey:@"Key3"];