文字列アイテムをリストの一番上に移動したい。NSMutableArrayでアイテムを移動するには?
NSMutableArray animal = "cat", "lion", "dog", "tiger";
犬をリストの先頭に移動するにはどうすればよいですか?
文字列アイテムをリストの一番上に移動したい。NSMutableArrayでアイテムを移動するには?
NSMutableArray animal = "cat", "lion", "dog", "tiger";
犬をリストの先頭に移動するにはどうすればよいですか?
あなたがアイテムを削除し、正しいスペースでそれを挿入します:
id tmp=[[animals objectAtIndex:2] retain];
[animals removeObjectAtIndex:2];
[animals insertObject:tmp atIndex:0];
[tmp release];
をあなたは、オブジェクトを保持するために持っているか、あなたはそれを削除するために、配列を言うとき、それはオブジェクトを解放します。
あなたはこのような何かを行うことができ、インデックスがわからない場合:
NSMutableArray* animals = [NSMutableArray arrayWithObjects:@"cat", @"lion", @"dog", @"tiger",nil];
for (NSString* obj in [[animals copy] autorelease]) {
if ([obj isEqualToString:@"dog"]) {
NSString* tmp = [obj retain];
[animals removeObject:tmp];
[animals insertObject:tmp atIndex:0];
break;
}
}
このメソッドは、すべてのあなたのリストの上に行くと、「犬」を検索し、見つかった場合、それはから削除されますされます
既存の要素(例:dog)を削除してから、配列の先頭に挿入し直すことができます。
NSMutableArray *animals = [NSMutableArray arrayWithObjects:@"cat", @"lion", @"dog", @"tiger",nil];
NSString *dog = @"dog";
// Check to see if dog is in animals
if ([animals containsObject:dog]) {
// Remove dog from animals and reinsert
// at the beginning of animals
[animals removeObject:dog];
[animals insertObject:dog atIndex:0];
}
私は、あなたの方法が非効率であることを指摘したいと思います。 NSMutableArrayからオブジェクトを削除/挿入することにより、削除/挿入後にすべての行に影響を与える可能性があります。私はAppleが可変配列を維持するためにどのような内部メソッドを使用するのかがはっきりしないので、「潜在的に」と言う。しかし、それが単純なc配列であると仮定すると、その削除/挿入インデックスの後のすべての行は下/上に移動する必要があります。非常に大きな配列では、移動された項目が先頭にある場合、これは非効率的である可能性があります。しかし、配列内の項目を置き換えることは、効率的ではありません。したがって、以下はNSMutableArrayの上のカテゴリがある(このコードは、ARCの下にあることに注意していないので、何のメモリ管理):また
- (void) moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex{
if (fromIndex == toIndex) return;
if (fromIndex >= self.count) return; //there is no object to move, return
if (toIndex >= self.count) toIndex = self.count - 1; //toIndex too large, assume a move to end
id movingObject = [self objectAtIndex:fromIndex];
if (fromIndex < toIndex){
for (int i = fromIndex; i <= toIndex; i++){
[self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : [self objectAtIndex:i + 1]];
}
} else {
id cObject;
id prevObject;
for (int i = toIndex; i <= fromIndex; i++){
cObject = [self objectAtIndex:i];
[self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : prevObject];
prevObject = cObject;
}
}
}
、小さなボーナスをさらに機能性を高めるために、あなたがアイテムに対して操作を実行している場合に移動( dbや何かを更新するのと同じように)、次のコードは私にとって非常に便利でした。
- (void) moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex withBlock:(void (^)(id, NSUInteger))block{
if (fromIndex == toIndex) return;
if (fromIndex >= self.count) return; //there is no object to move, return
if (toIndex >= self.count) toIndex = self.count - 1; //toIndex too large, assume a move to end
id movingObject = [self objectAtIndex:fromIndex];
id replacementObject;
if (fromIndex < toIndex){
for (int i = fromIndex; i <= toIndex; i++){
replacementObject = (i == toIndex) ? movingObject : [self objectAtIndex:i + 1];
[self replaceObjectAtIndex:i withObject:replacementObject];
if (block) block(replacementObject, i);
}
} else {
id cObject;
id prevObject;
for (int i = toIndex; i <= fromIndex; i++){
cObject = [self objectAtIndex:i];
replacementObject = (i == toIndex) ? movingObject : prevObject;
[self replaceObjectAtIndex:i withObject:replacementObject];
prevObject = cObject;
if (block) block(replacementObject, i);
}
}
}
このメソッドはO(n)のように見えますが、O(1) – OpenThread
インデックスの代わりに文字列? – HardCode
@HardCodeはい、ループを見つけるだけです。私はそれを反映するために私の答えを更新します – utahwithak
ありがとうございました。 – HardCode