简体   繁体   English

如何在目标c中执行移位和推动操作

[英]how to do shift and push operation in objective c

I have an array [1,2,3,4] 我有一个数组[1,2,3,4]

I need to continously shift and push one element into the same array 我需要不断地移动并将一个元素推入同一数组

so that the first variable values will be 1,2,3,4,1,2,3,4,... 这样第一个变量值将是1,2,3,4,1,2,3,4,...

and second variable values will be 2,3,4,1,2,3,4,1,... 第二个变量值为2,3,4,1,2,3,4,1,...

How to do that?? 怎么做??

Use NSMutableArray like: 使用NSMutableArray如:

1) to get the object at the first position: 1)将对象放在第一个位置:

object = [nameArray firstObject];
[nameArray removeObjectAtIndex:0];

2) then insert it in last position: 2)然后将其插入到最后一个位置:

[nameArray addObject:object];

ps remember to always add some sanity checks. ps记得总是添加一些健全性检查。

You would need 你需要

- exchangeObjectAtIndex:withObjectAtIndex:

Exchanges the objects in the array at given indices. 以给定的索引交换数组中的对象。

See Documentation here 在这里查看文档

Example

NSMutableArray *array = [NSMutableArray array];
    [array setArray:@[@"Eezy",@"Tutorials",@"Website"]];
    [array exchangeObjectAtIndex:0 withObjectAtIndex:2];
NSLog(@"%@",array); 

NSMutableArray *obj = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];
    [obj exchangeObjectAtIndex:0 withObjectAtIndex:obj.count - 1];

 NSLog(@"%@",obj);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM