我有一個NSMutableArray的叫組織這是I類從NSObject的子類的自定義對象的數組,這個子類被稱爲NSXOrganisation。這個子類實現NSCopy像這樣:與獲取和改變NSMutableArray的問題對象
@interface NSXOrganisation : NSObject <NSCopying>
...
- (id)copyWithZone:(NSZone *)zone
{
id copy = [[[self class] alloc] init];
if (copy)
{
[copy setPropertyOne:[[[self getPropertyOne] copyWithZone:zone] autorelease]];
}
return copy;
}
現在,我的Alloc &數組初始化像這樣:
organisations = [[NSMutableArray alloc] initWithArray:organisationsArray copyItems:YES];
這一切工作正常。爲了測試,陣列中有2個對象。
接着,當方法被調用時,我更新所述陣列中的物體,像這樣:
- (void)updateOrganisation
{
[[organisations objectAtIndex:0] setConsumerKey:[tfPropertyOne stringValue]];
}
凡tfPropertyOne *是的NSTextField在顯示器上(綁定正確等)。這也工作正常,因爲我已經用NSLog測試過,並輸出正確的結果。然後我請不同的索引的顯示方法在陣列中,以查看其相關的詳細信息:
- (void)displayOrganisation:
{
[tfPropertyOne setStringValue:[[organisations objectAtIndex:index] getPropertyOne]];
}
索引是本地NSInteger的並且是沒有問題的。然後我對這個對象調用相同的更新方法,這顯然工作正常(使用NSLog測試)。
最後,當我打個電話回去,並在陣列顯示的第一個對象[自displayOrganisation:0]通話成功的作品,但我的應用程序崩潰調用getPropertyOne時。
這是Mac的控制檯錯誤:
...: Invalid memory access of location 0x3bcfd069 eip=0x90c1cd5c
這是從崩潰報告:
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libobjc.A.dylib 0x90c1cd4b objc_msgSend + 27
1 com.apple.Foundation 0x943da945 _NSDescriptionWithLocaleFunc + 57
2 com.apple.CoreFoundation 0x906503ae __CFStringAppendFormatCore + 12238
3 com.apple.CoreFoundation 0x906a70dc _CFStringCreateWithFormatAndArgumentsAux + 108
4 com.apple.CoreFoundation 0x9072821c _CFLogvEx + 124
5 com.apple.Foundation 0x9445d221 NSLogv + 136
6 com.apple.Foundation 0x9445d194 NSLog + 27
7 co.nz.fdl.XeroConnect 0x1b5b4872 -[PrefsController displayOrganisation:] + 194 (PrefsController.mm:229)
誰能幫助?我是否正確訪問/創建/將對象存儲到數組中?
我沒有在我的代碼autorelease調用,所以它不是那麼不幸。 – Aleks 2012-07-27 01:08:32