2016-03-29 19 views
0

の特定のインデックスのテキストフィールドの内容を追加する必要があります。は、私はこのコードを持っているNSMutableArrayの

 NSMutableArray *customServicesArray = [[NSMutableArray alloc] initWithCapacity: 24]; 
    PreferenceData *userPreferences = [PreferenceData MR_findFirstInContext: [NSManagedObjectContext MR_defaultContext]]; 

    // fill the array with existing data (if any) 
    customServicesArray = [NSMutableArray arrayWithObjects: userPreferences.aCustomServices1, userPreferences.aCustomServices2, userPreferences.aCustomServices3, userPreferences.aCustomServices4, userPreferences.aCustomServices5, userPreferences.aCustomServices6, userPreferences.aCustomServices7, userPreferences.aCustomServices8, userPreferences.aCustomServices9, userPreferences.aCustomServices10, userPreferences.aCustomServices11, userPreferences.aCustomServices12, userPreferences.aCustomServices13, userPreferences.aCustomServices14, userPreferences.aCustomServices15, userPreferences.aCustomServices16, userPreferences.aCustomServices17, userPreferences.aCustomServices18, userPreferences.aCustomServices19, userPreferences.aCustomServices20, userPreferences.aCustomServices21, userPreferences.aCustomServices22, userPreferences.aCustomServices23, userPreferences.aCustomServices24,nil]; 

    NSInteger indexSelected = [oCustomServicesPickerView selectedRowInComponent:0]; 
    if(customServicesArray.count > (unsigned long)indexSelected) 
     [customServicesArray replaceObjectAtIndex:indexSelected withObject:textField.text]; // check to see max number of objects; might want to insert object then 
    else 
     [customServicesArray insertObject:textField.text atIndex: indexSelected]; 

私は特定のインデックスでcustomServicesArrayに要素を追加する必要があります。配列が空の場合は明らかに私が持っているものは動作していません。私はaddObjectを試しましたが、インデックスの欠如のために動作しません。

この作業を希望通りに行うにはどうすればよいですか?

+0

のようないくつかのコードを追加[CocoaのNSMutableArrayのは、スパースですか?](http://stackoverflow.com/q/6647518) –

答えて

2

あなたは可能な最大値がわかっている場合は、[NSNull null]または他のいくつかのダミーオブジェクトを使用して、配列を事前入力することができます。

か、その

NSInteger indexSelected = [oCustomServicesPickerView selectedRowInComponent:0]; 

while (customServicesArray.count <= indexSelected) { 
    [customServicesArray addObject:[NSNull null]]; // Here you can add some dummy object 
} 

[customServicesArray replaceObjectAtIndex:indexSelected withObject: @"textFieldText"]; 
1

代わりにNSMutableDictionaryを使用してください。

customServicesDictionary[@(indexSelected)] = textField.text; 
+0

が、その私の唯一のオプションですか? * customServicesArray *がNSMutableArrayであることに依存する多くのコードがあります。 – SpokaneDude

関連する問題