Objective c 如何获取阵列的每4个元素?
我的问题是,我有一个名为Objective c 如何获取阵列的每4个元素?,objective-c,nsmutablearray,nsarray,Objective C,Nsmutablearray,Nsarray,我的问题是,我有一个名为latLongArray的NSMutableArray,如果数组计数大于4,它将包含对象 我想在另一个名为elementArray的数组中添加每4个对象,如果少于4个,我想在elementArray中添加所有对象,然后在NSLog中打印所有数据 有人能帮我吗?我对iPhone的开发还不熟悉。 这是我的密码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)in
latLongArray
的NSMutableArray
,如果数组计数大于4,它将包含对象
我想在另一个名为elementArray的数组中添加每4个对象,如果少于4个,我想在elementArray中添加所有对象,然后在NSLog中打印所有数据
有人能帮我吗?我对iPhone的开发还不熟悉。
这是我的密码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
placeDC *placedc;
placedc = [dataArray objectAtIndex:[indexPath row]];
if ([txtFrom isFirstResponder]) {
txtFrom.text=cellText;
NSLog(@"%@",cellText);
originlat=placedc.latitude;
originlng=placedc.longitude;
NSLog(@"%@",placedc.countryFullName);
}
else
{
txtTo.text=cellText;
destinationLat=placedc.latitude;
DestinationLng=placedc.longitude;
DBHandler *db=[[DBHandler alloc]init];
NSLog(@"%@",placedc.countryFullName);
latLongArray=[db googlePlaces:originlat :originlng :destinationLat :DestinationLng];
if ([latLongArray count]>4) {
for (int i =0; i<=[latLongArray count]; i=i+4) {
}
NSLog(@"%d",[elentArray count]);
}
}
myTableView.hidden=YES;
[self.view endEditing:YES];
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
{
UITableViewCell*selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSString*cellText=selectedCell.textlab.text;
placeDC*placeDC;
placedc=[dataArray objectAtIndex:[indexPath行]];
if([txtfromisfirstresponder]){
txtFrom.text=cellText;
NSLog(@“%@”,cellText);
原始纬度=地方纬度;
originlng=placedc.经度;
NSLog(@“%@”,placedc.countryFullName);
}
其他的
{
text=cellText;
destinationLat=地点DC.纬度;
destinationling=placedc.longitude;
DBHandler*db=[[DBHandler alloc]init];
NSLog(@“%@”,placedc.countryFullName);
latLongArray=[db googlePlaces:originlat:originlng:destinationLat:destinationLat];
如果([latLongArray count]>4){
对于(int i=0;i只需更改:
NSMutableArray *newArrayOfFourthElement = [[NSMutableArray alloc] init];
elementArray = [[NSMutableArray alloc] init];
int j = 3;
if ([latLongArray count]>4)
{
for (int i =0; i <= [latLongArray count]; i=i+4)
{
if (j < [latLongArray count])
{
[newArrayOfFourthElement addObject:[latLongArray objectAtIndex:j]]; // This line will add all the forth element(i.e. 4,8,12....) from your latLongArray to newArrayOfFourthElement array.
}
j = j + 4;
}
}
else
{
elementArray = [[NSMutableArray alloc]initWithArray:latLongArray];
}
NSLog("New array is %@",newArrayOfFourthElement);
NSLog("elementArray is %@",elementArray);
NSMutableArray*newArrayOffourtheElement=[[NSMutableArray alloc]init];
elementArray=[[NSMutableArray alloc]init];
int j=3;
如果([latLongArray count]>4)
{
对于(int i=0;i-(void)数组示例{
NSMutableArray*newArray=[[NSMutableArray alloc]init];
如果(_longArray.count>4){
int lastObjectIndex=\u longArray.count-1;
int-nextIndexToAd=3;
while(nextIndexToAd如果要添加第四个元素
NSMutableArray *elementArray = [[NSMutableArray alloc] init];
if ([latLongArray count]>4)
{
//add 4th element mean object at index 3
[elementArray addObject:[latLongArray objectAtIndex:3]];
}
else{
elementArray=[latLongArray mutableCopy];
}
NSLog(@"elentArray array >> %@ and count = %d",elentArray,[elentArray count]);
如果你想增加每四个元素
NSMutableArray *latLongArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
NSMutableArray *elementArray = [[NSMutableArray alloc] init];
if ([latLongArray count]<4)
{
elementArray=[latLongArray mutableCopy];
}
else{
do {
[elementArray addObject:[latLongArray objectAtIndex:3]];
[latLongArray removeObjectsInRange:(NSRange){0, 4}];
} while ([latLongArray count]>4);
}
NSLog(@"%@",elementArray);
NSMutableArray*latLongArray=[[NSMutableArray alloc]initWithObjects:@“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,nil];
NSMutableArray*elementArray=[[NSMutableArray alloc]init];
if([latLongArray count]4);
}
NSLog(@“%@”,elementArray);
非工作元素数组计数0非工作元素数组计数0@Da_smokes谢谢:)Saleem Last nslog给你0?是的@Rushi ns log给我0和thaksrply@Saleem在您的代码中,您在哪里声明elentArray数组?哦,问题是您需要在复制任何内容之前允许elementArray。只需在for循环和检查之前允许它。请参阅我编辑了我的代码。我想添加每4个元素,以便添加第四个元素。这有什么问题吗?谢谢@Saleem如果你有任何问题,然后问:)看到这个链接,@Rushi你的答案会崩溃…检查
NSMutableArray *latLongArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
NSMutableArray *elementArray = [[NSMutableArray alloc] init];
if ([latLongArray count]<4)
{
elementArray=[latLongArray mutableCopy];
}
else{
do {
[elementArray addObject:[latLongArray objectAtIndex:3]];
[latLongArray removeObjectsInRange:(NSRange){0, 4}];
} while ([latLongArray count]>4);
}
NSLog(@"%@",elementArray);