今天發生了一件奇怪的事情。爲什麼NSMutableArray文字賦值不能正確工作?
我有一個的NSMutableArray陣列
@property (nonatomic) NSMutableArray *draggedWords;
然後我分配空值的陣列(someElements有4種元素)
[someElements enumerateObjectsUsingBlock:^(CustomLabel *element, NSUInteger idx, BOOL *stop) {
// ...
self.draggedWords[idx] = [NSNull null];
}];
NSLog(@"%@", self.draggedWords);
然後,在同一個控制器的另一個動作,我做更換值(索引== 0)
self.draggedWords[index] = self.draggableItem;
NSLog(@"%@", self.draggedWords);
結果實際上很奇怪。相反,在指數來取代值,它是將其添加:
2014-02-14 14:21:44.601 [39271:70b] (
"<null>",
"<null>",
"<null>",
"<null>"
)
2014-02-14 14:21:49.870 [39271:70b] (
"<CustomLabel: 0x19156b40; baseClass = UILabel; frame = (374 0; 300 80); text = '...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x191d0960>>",
"<null>",
"<null>",
"<null>",
"<null>"
)
如果我使用replaceObjectAtIndex相反,它按預期工作
[array replaceObjectAtIndex:0 withObject:Object];
我得到了我的預期(4元)
2014-02-14 14:25:24.670 [39271:70b] (
"<CustomLabel: 0x19156b40; baseClass = UILabel; frame = (374 0; 300 80); text = '...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x191d0960>>",
"<null>",
"<null>",
"<null>"
)
爲什麼地球上會發生這種情況? 我期待着同樣的行爲。
我懷疑你說的是真的。有可能得到一個「困惑」的數組,其中(由於不正確的存儲管理)某些元素處理不存在的對象。這可能會導致奇怪的行爲。 –
(你應該使用術語「賦值」,「賦值」與大多數說英語的人有完全不同的含義)。 –
我對你的結果有懷疑,因爲我的測試與你的不同。請顯示您的代碼 – scorpiozj