Reputation: 53
Hi is there a way to make an array by jumping over every 1 index like lets say i have an array with objects like this:
NSArray *array =[NSArray alloc] initWithObjects:@"id",@"name",@"id",@"name",@"id",@"name"..
... and so on, can i make an array by taking only the ID objects and/or NAMES. I'm stuck in this, what i really want to achieve is i want id's to be in the tableview subtitle, and names on tableview title. any suggestions?
EDIT: Thanks you for your help everybody, already got my answer, i couldn't go though NSDictionary because, i'm getting objects from .php. Anyways thank you!
Upvotes: 0
Views: 363
Reputation: 53010
Many answers will just suggest using some other data structure which is recommended if you can. But assuming you have such an array and need to do this its not hard. You could process the array into two - potentially a lot of copying; or you could simply take a view of the array - which trades a smidgin in access time for copying. Here is JASArray
with absolutely no checking code (that is left to you):
@interface JASArray : NSArray
- (id) initWith:(NSArray *)baseArray jumpTo:(NSUInteger)jumpTo strideBy:(NSUInteger)strideBy;
@end
To override NSArray
you only need to implement count
and objectAtIndex:
:
@implementation JASArray
{
NSUInteger jump;
NSUInteger stride;
NSArray *base;
}
- (id) initWith:(NSArray *)baseArray jumpTo:(NSUInteger)jumpTo strideBy:(NSUInteger)strideBy
{
self = [super init];
if (self)
{
base = baseArray;
jump = jumpTo;
stride = strideBy;
}
return self;
}
- (NSUInteger) count
{
return 1 + (base.count - jump - 1) / stride;
}
- (id) objectAtIndex:(NSUInteger)index
{
return [base objectAtIndex:(jump + index * stride)];
}
@end
Now a test, note the on purpose mismatch pairs as a test:
NSArray *array =@[ @"id1", @"name1", @"id2", @"name2", @"id3", @"name3", @"id4" ];
JASArray *ids = [[JASArray alloc] initWith:array jumpTo:0 strideBy:2];
JASArray *names = [[JASArray alloc] initWith:array jumpTo:1 strideBy:2];
NSLog(@"%@\n%@\n%@", array, ids, names);
Output:
JumpAndStride[10480:303] (
id1,
name1,
id2,
name2,
id3,
name3,
id4
)
(
id1,
id2,
id3,
id4
)
(
name1,
name2,
name3
)
HTH
Upvotes: -1
Reputation: 16536
If you want ids and names separated in two different arrays, you could iterate through the original array, and use mod operator like this.-
NSArray *array = [[NSArray alloc] initWithObjects:@"id",@"name",@"id",@"name",@"id",@"name", nil];
NSMutableArray *ids = [[NSMutableArray alloc] init];
NSMutableArray *names = [[NSMutableArray alloc] init];
for (int i = 0; i < array.count; i ++) {
if (i % 2 == 0) {
[ids addObject:[array objectAtIndex:i]];
} else {
[names addObject:[array objectAtIndex:i]];
}
}
Upvotes: 1
Reputation: 10201
You can easily achieve by making an array of dictionary objects.
NSArray *array = @[@{@"Id":1, @"Name":@"UserName1"},@"Id":2, @"Name":@"UserName2"}];
And in the cellForRowAtIndexPath
: method
NSDictionary *user = self.array[indexPath.row];
cell.textLabel.text = user[@"Id"];
cell.detailLabel.text = user[@"Name"];
Upvotes: 2
Reputation: 108159
You have two options
First (naive) one:
cell.subtitle.text = array[indexPath.row*2];
cell.title.text = array[indexPath.row*2+1];
Anyway, a more correct alternative would be to use an array of NSDictionary
s
NSArray *array = [NSArray alloc] initWithObjects:@{
@"id": @"theId", @"name" : @"theName"},
@"id": @"anotherId", @"name" : @"anotherName"},
...];
And then
cell.subtitle.text = array[indexPath.row][@"id"];
cell.title.text = array[indexPath.row][@"name"];
Upvotes: 2
Reputation: 7719
Why not use 2 arrays from the start? If you have to split the arrays, an easy way would be to do it on even and odd numbers.
NSArray *array =[[NSArray alloc] initWithObjects:@"id",@"name",@"id",@"name",@"id",@"name",nil];
NSArray *ids = [array objectsAtIndexes:[array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return ((idx % 2) == 0);
}]];
NSArray *names = [array objectsAtIndexes:[array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return ((idx % 2) != 0);
}]];
Upvotes: 1