2014-02-14 42 views
-1

今天發生了一件奇怪的事情。爲什麼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>" 
) 

爲什麼地球上會發生這種情況? 我期待着同樣的行爲。

+1

我懷疑你說的是真的。有可能得到一個「困惑」的數組,其中(由於不正確的存儲管理)某些元素處理不存在的對象。這可能會導致奇怪的行爲。 –

+0

(你應該使用術語「賦值」,「賦值」與大多數說英語的人有完全不同的含義)。 –

+1

我對你的結果有懷疑,因爲我的測試與你的不同。請顯示您的代碼 – scorpiozj

回答

4

最少測試用例:

NSMutableArray *foo = [NSMutableArray arrayWithObjects:[NSNull null], [NSNull null], [NSNull null], [NSNull null], nil]; 
NSLog(@"%@", foo); 
foo[0] = @"bar"; 
NSLog(@"%@", foo); 

輸出:

2014-02-14 14:27:15.083 test[6642:70b] (
    "<null>", 
    "<null>", 
    "<null>", 
    "<null>" 
) 
2014-02-14 14:27:15.084 test[6642:70b] (
    bar, 
    "<null>", 
    "<null>", 
    "<null>" 
) 

所以你描述並非來自可可的行爲。請顯示你的代碼。

編輯

再次,不可能重現你給了什麼問題:

NSArray *someElements = @[@1, @2, @3, @4]; 

NSMutableArray *draggedWords = [NSMutableArray array]; 
NSLog(@"%@", draggedWords); 
[someElements enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    draggedWords[idx] = [NSNull null]; 
}]; 

NSLog(@"%@", draggedWords); 

draggedWords[0] = @"bar"; 
NSLog(@"%@", draggedWords); 

輸出:

2014-02-14 17:03:07.427 test[15380:70b] (
) 
2014-02-14 17:03:07.429 test[15380:70b] (
    "<null>", 
    "<null>", 
    "<null>", 
    "<null>" 
) 
2014-02-14 17:03:07.429 test[15380:70b] (
    bar, 
    "<null>", 
    "<null>", 
    "<null>" 
) 

東西是錯在你的代碼的其他部分。嘗試通過按位評論您的代碼位,直到錯誤消失,來確定哪個部分會給您這種奇怪的輸出。

+0

這就是我所期望的,但它發生了一些不同的事情。我用我的應用程序中的實際代碼行更新了這個問題。 – jollyr0ger

+0

@ jollyr0ger編輯了我的答案。 –

+0

我並沒有改變我所羨慕的同一個對象。我在枚舉someElements,同時編輯self.draggedWords(與someElements的索引,它不是一回事) – jollyr0ger

0

你有點困惑。此代碼:

NSMutableArray *array = [NSMutableArray arrayWithObjects: @"zero", @"one", @"two", @"three", nil]; 
array[0] = @"new one"; 
NSLog(@"array = %@", array); 

給出了這樣的結果:

array = (
    "new one", 
    one, 
    two, 
    three 
) 

這清楚地表明,在指數爲0的項目被替換爲「新的」。

+0

這就是我期望的,但它發生了一些不同的事情。我在應用程序中的實際代碼行。 – jollyr0ger