hello i am new to IOS dev, and i need to create the contacts like view, i was able to create it if i already have the arrays of contacts and indexes, but what i need is the get the contacts from the server and sort them with indexes with objective C, any help?
i checked this tutorial:
animals = #{#"B" : #[#"Bear", #"Black Swan", #"Buffalo"],
#"C" : #[#"Camel", #"Cockatoo"],
#"D" : #[#"Dog", #"Donkey"],
#"E" : #[#"Emu"],
#"G" : #[#"Giraffe", #"Greater Rhea"],
#"H" : #[#"Hippopotamus", #"Horse"],...
sectionTitles = [[animals allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
animalIndexTitles = #[#"A", #"B", #"C", #"D", #"E", #"F", #"G", #"H", #"I", #"J", #"K", #"L", #"M", #"N", #"O", #"P", #"Q", #"R", #"S", #"T", #"U", #"V", #"W", #"X", #"Y", #"Z"];
but obviously this isn't what i need,
First you have to sort all keys :
NSArray *keys = [animals allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSString *first = [someDictionary objectForKey:a];
NSString *second = [someDictionary objectForKey:b];
return [first compare:second];
}];
after sorting keys you have to sort each array against keys .
for( i= 0 ; i < [sortedKeys count ]; i++){
NSArray * arr = [animals objectForKey:[sortedKeys objectAtIndex: i] ];
NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[obj1 valueForKey:#"value"] compare:[obj2 valueForKey:#"value"]];
}];
Related
I am new in iOS.And I am doing like this
NSMutableArray* arr1 = [NSMutableArray arrayWithObjects: #"A", #"C", #"E", nil];
NSMutableArray* arr2 = [NSMutableArray arrayWithObjects: #"B", #"D", #"F", nil];
NSMutableArray* animals = [NSMutableArray arrayWithArray:arr1];
[animals addObjectsFromArray: arr2];
It give me output like
ACEBDE
But I need output like
ABCDEF
Any Hint
Just loop them
NSMutableArray* arr1 = [NSMutableArray arrayWithObjects: #"A", #"C", #"E", nil];
NSMutableArray* arr2 = [NSMutableArray arrayWithObjects: #"B", #"D", #"F", nil];
NSMutableArray* animals = [NSMutableArray new];
NSUInteger maxCount = arr1.count > arr2.count ? arr1.count : arr2.count;
for (int i = 0; i < maxCount; i ++) {
if ([arr1 objectAtIndex:i]) {
[animals addObject:[arr1 objectAtIndex:i]];
}
if ([arr2 objectAtIndex:i]) {
[animals addObject:[arr2 objectAtIndex:i]]
}
}
This will work for all types of arrays.
I tried now.I got it.Check the below answer.You have to use sortDescriptor simply.
NSMutableArray* arr1 = [NSMutableArray arrayWithObjects: #"A", #"C", #"E", nil];
NSMutableArray* arr2 = [NSMutableArray arrayWithObjects: #"B", #"D", #"F", nil];
NSMutableArray* animals = [NSMutableArray arrayWithArray:arr1];
[animals addObjectsFromArray: arr2];
NSArray *sortedArray = [animals sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSLog(#"The sortedArrays are - %#",sortedArray);
The printed results are
The sortedArrays are - (
A,
B,
C,
D,
E,
F
)
try this -
NSMutableArray* arr1 = [NSMutableArray arrayWithObjects: #"A", #"C", #"E", nil];
NSMutableArray* arr2 = [NSMutableArray arrayWithObjects: #"B", #"D", #"F", nil];
NSUInteger maxCount = arr1.count > arr2.count ? arr1.count : arr2.count;
NSMutableArray* animals = [[NSMutableArray alloc] init];
for (int i=0; i<maxCount; i++)
{
if (i<arr1.count) {
[animals addObject:[arr1 objectAtIndex:i]];
}
if (i<arr2.count) {
[animals addObject:[arr2 objectAtIndex:i]];
}
}
Adding another answer just for fun.
NSMutableArray* arr1 = [NSMutableArray arrayWithObjects: #"A", #"C", #"E", nil];
NSMutableArray* arr2 = [NSMutableArray arrayWithObjects: #"B", #"D", #"F", nil];
NSMutableArray* animals = [NSMutableArray new];
NSInteger minCount;
NSMutableArray *maxArray;
if (arr1.count < arr2.count) {
minCount = arr1.count;
maxArray = arr2;
}
else {
minCount = arr2.count;
maxArray = arr1;
}
for (int i = 0; i < minCount; i++) {
[animals addObject:arr1[i]];
[animals addObject:arr2[i]];
}
NSInteger pendingItemsLength;
if ((pendingItemsLength = maxArray.count - minCount)) {
[animals addObjectsFromArray:[maxArray subarrayWithRange:NSMakeRange(minCount, pendingItemsLength)]];
}
This is efficient than the accepted answer as it avoids if condition inside a for loop.
Imagine I have the following NSDictionary
dict = {
"a" = [
obj1,
obj2,
obj3
],
"b" = [
obj4,
obj5
],
"invalid" = [
obj6,
obj7,
obj8
],
"c" = [
obj9,
obj10,
obj11
]
}
This data is used to populate a TableView using sections from the following NSArray:
arr = #[#"A", #"B", #"C", #"D", #"E", #"F", #"G", #"H", #"I", #"J", #"K", #"L", #"M", #"N", #"O", #"P", #"Q", #"R", #"S", #"T", #"U", #"V", #"W", #"X", #"Y", #"Z"];
I have the following method which I use to find an object
- (void)selectRowWithId:(NSNumber *)uid {
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(id == %#)", uid];
NSArray *filteredDictArray = [obj filteredArrayUsingPredicate:predicate];
if ([filteredDictArray count] > 0) {
NSIndexPath *targetIndexPath = nil;
//trying to find the NSIndexPath here
[[self tableView] selectRowAtIndexPath:targetIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:[self tableView] didSelectRowAtIndexPath:targetIndexPath];
}
}];
}
Say I have an object set to obj10.
Based on my NSDictionary and NSArray the NSIndexPath is Section:2 Row:1
How can I get this value if I only know obj10?
Update (Solution)
So With a combination of the ideas behind everyone's answers and my own here is the following I used in case it's helpful for someone. BTW: This is for iOS9
- (void)selectRowWithId:(NSNumber *)uid {
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(id == %#)", uid];
NSArray *filteredDictArray = [obj filteredArrayUsingPredicate:predicate];
if ([filteredDictArray count] > 0) {
//trying to find the NSIndexPath here
NSIndexPath *targetIndexPath = [NSIndexPath indexPathForRow:[[dict objectForKey:key] indexOfObject:filteredPersonsArray[0]] inSection:[arr indexOfObject:key]];
[[self tableView] selectRowAtIndexPath:targetIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:[self tableView] didSelectRowAtIndexPath:targetIndexPath];
}
}];
}
Your code is a little bit too tricky. Sometimes usual looping is easier.
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL * stop)
{
for( NSInteger index = 0; index < [obj count]; index++ )
{
if ([obj[index] isEqualToString:uid])
{
// thats the index
// Let's have a look-up for the section
NSString *sectionKey = [[key substringToIndex:1] uppercaseString]; //Typo
for( NSInteger section=0; section < [arr count]; section++ )
{
if( [arr[section] isEqualToString:sectionKey] )
{
// That's the section
}
}
}
}
}
Typed in Safari.
I have multiple arrays, for example:
NSArray *a = #[#"a", #"b", #"c"];
NSArray *b = #[#"d", #"a", #"e"];
NSArray *c = #[#"i", #"f", #"a"];
As you can see "a" is exist in array a, b, c. I would like to make a function that return the same objet in supplied arrays. So, like this one, I want to get this "a" from them. if all arrays don't have the same object, which will return nil. For example "f" only exist in c, so the function should return nil.
NSMutableSet *set = [NSMutableSet new];
NSMutableSet *set1 = [NSMutableSet setWithArray:a];
NSMutableSet *set2 = [NSMutableSet setWithArray:b];
NSMutableSet *set3 = [NSMutableSet setWithArray:c];
set = [set1 intersectSet:set2];
set = [set intersectSet:set3];
NSArray *allArray = [set allObjects];
NSMutableSet *intersection = [NSMutableSet setWithArray:a];
[intersection intersectSet:[NSSet setWithArray:b]];
[intersection intersectSet:[NSSet setWithArray:c]];
NSArray *intersecArray = [intersection allObjects];
this work! from your code it return a in result array
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *a = #[#"a", #"b", #"c"];
NSArray *b = #[#"d", #"a", #"e"];
NSArray *c = #[#"i", #"f", #"a"];
NSArray *arraydOfAll=[NSArray arrayWithObjects:a,b,c,nil];
NSArray *commonObjArray=[self intersectArray:arraydOfAll];
}
-(NSArray *)intersectArray:(NSArray *)allArray{
NSMutableSet *set1 = [NSSet setWithArray:[allArray objectAtIndex:0]];
for (NSInteger i=1;i<allArray.count : i++){
[set1 intersectSet:[NSSet setWithArray:[allArray objectAtIndex:i]];
}
return [set1 allObjects]
}
i am new to ios and i am really stuck at finding solution for this...
animals = #{#"B" : #[#"Bear", #"Black Swan", #"Buffalo"],
#"C" : #[#"Camel", #"Cockatoo"],
#"D" : #[#"Dog", #"Donkey"],
#"E" : #[#"Emu"],
#"G" : #[#"Giraffe", #"Greater Rhea"],
#"H" : #[#"Hippopotamus", #"Horse"],
#"K" : #[#"Koala"],
#"L" : #[#"Lion", #"Llama"],
#"M" : #[#"Manatus", #"Meerkat"],
#"P" : #[#"Panda", #"Peacock", #"Pig", #"Platypus", #"Polar Bear"],
#"R" : #[#"Rhinoceros"],
#"S" : #[#"Seagull"],
#"T" : #[#"Tasmania Devil"],
#"W" : #[#"Whale", #"Whale Shark", #"Wombat"]};
animalSectionTitles = [[animals allKeys] sortedArrayUsingSelector:#selector(compare:)];
the code above doesn't error but when i trying to insert array into NSDictionary, it always shows unrecognized selecter. What should i do to solve this problem?
while (sqlite3_step(statement)==SQLITE_ROW) {
Word *univer = [[Word alloc] init];
univer.Id =[NSString stringWithUTF8String:(char *) sqlite3_column_text(statement,0)];
univer.word=[NSString stringWithUTF8String:(char *) sqlite3_column_text(statement,1)];
//NSLog(#"%#",univer.word);
NSString *first = [univer.word substringToIndex:1];
NSLog(#"%#",first);
if([first isEqualToString:#"A"] || [first isEqualToString:#"a"]){
[_A addObject:univer.word];
}
else if([first isEqualToString:#"B"] || [first isEqualToString:#"b"]){
[_B addObject:univer.word];
}
else if([first isEqualToString:#"C"] || [first isEqualToString:#"c"]){
[_C addObject:univer.word];
}
else if([first isEqualToString:#"D"] || [first isEqualToString:#"d"]){
[_D addObject:univer.word];
}
else if([first isEqualToString:#"E"] || [first isEqualToString:#"e"]){
[_E addObject:univer.word];
}
else if([first isEqualToString:#"F"] || [first isEqualToString:#"f"]){
[_F addObject:univer.word];
}
else if([first isEqualToString:#"G"] || [first isEqualToString:#"g"]){
[_G addObject:univer.word];
}
else if([first isEqualToString:#"H"] || [first isEqualToString:#"h"]){
[_H addObject:univer.word];
}
}
animals= [NSDictionary dictionaryWithObjectsAndKeys:_A,_B,_C,_D,_E,_F,_G,_H, nil];
animalSectionTitles = [[animals allKeys] sortedArrayUsingSelector:#selector(compare:)];
The problem is this line:
animals= [NSDictionary dictionaryWithObjectsAndKeys:_A,_B,_C,_D,_E,_F,_G,_H, nil];
When you create a dictionary with dictionaryWithObjectsAndKeys: the list of items you supply have to be objectA, keyA, objectB, keyB, objectC, keyC, nil. Right now it looks like all you have is objectA, objectB, objectC, nil. Which looks like this if you write it out:
animals = #{#[#"Bear", #"Black Swan", #"Buffalo"] : #[#"Camel", #"Cockatoo"],
#[#"Dog", #"Donkey"] : #[#"Emu"],
#[#"Giraffe", #"Greater Rhea"] : #[#"Hippopotamus", #"Horse"]};
When you call [[animals allKeys] sortedArrayUsingSelector:#selector(compare:)] it's calling the compare: selector on all of the items in [animals allKeys] which are NSArrays which don't support the compare: method and thus the unrecognized selector error.
Not sure exactly what you want but try this:
animals= [NSDictionary dictionaryWithObjectsAndKeys:_A, #"A", _B, #"B", _C, #"C", nil];
I have one NSMutableArray *arr1 with values:
{(B,abc) (E,pqr) (C,xyz)}
and another NSMutableArray *arr2 with
{(B) (C) (E)}.
Now i want to sort arr1 using arr2 value so that arr1 becomes {(B,abc) (C,xyz) (E,pqr)}. How can i do this?
So, it seems you have an array and an array of arrays:
NSArray *sorter = #[#"B", #"C", #"E"];
NSMutableArray *sortee = [#[
#[#"B", #"abc"],
#[#"E", #"pqr"],
#[#"C", #"xyz"]
] mutableCopy];
[sortee sortUsingComparator:^(id o1, id o2) {
NSString *s1 = [o1 objectAtIndex:0];
NSString *s2 = [o2 objectAtIndex:0];
NSInteger idx1 = [sorter indexOfObject:s1];
NSInteger idx2 = [sorter indexOfObject:s2];
return idx1 - idx2;
}];
Try this,
NSMutableArray *unsortedArray = [NSMutableArray arrayWithObjects:[NSArray arrayWithObjects:#"B",#"abc", nil],[NSArray arrayWithObjects:#"E",#"pqr", nil],[NSArray arrayWithObjects:#"C",#"xyz", nil],nil];
NSArray *guideArray = [NSArray arrayWithObjects:#"B",#"C",#"E", nil];
for(int i=0; i< [guideArray count];i++)
{
for(int j=0; j< [unsortedArray count];j++)
{
if([[unsortedArray objectAtIndex:j] containsObject:[guideArray objectAtIndex:i]])
{
[unsortedArray exchangeObjectAtIndex:j withObjectAtIndex:i];
break;
}
}
}
NSLog(#"%#",unsortedArray);
I have tested this and working for me. Hope this helps you.