Create an NSDictionary containing several NSArrays - objective-c

I want to unite several NSArrays into one NSDictionary.
Here are my arrays:
#property (nonatomic, strong) NSArray *EuropeTable;
#property (nonatomic, strong) NSArray *AsiaTable;
#property (nonatomic, strong) NSArray *AfricaTable;
#property (nonatomic, strong) NSArray *LatinAmericaTable;
#property (nonatomic, strong) NSArray *NorthAmericaTable;
Any ideas on how to do this?
*EDIT
I want each array to have the same key #"country", so that I could use this single key later

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:self.EuropeTable forKey:#"EuropeTable"];
[dictionary setObject:self.AsiaTable forKey:#"AsiaTable"];
[dictionary setObject:self.AfricaTable forKey:#"AfricaTable"];
[dictionary setObject:self.LatinAmericaTable forKey:#"LatinAmericaTable"];
[dictionary setObject:self.NorthAmericaTable forKey:#"NorthAmericaTable"];
Then to access one of the arrays you need to:
NSArray *europeTable = [dictionary objectForKey:#"EuropeTable"];
UPDATE:
NSArray *array = [NSArray arrayWithObjects:self.EuropeTable,
self.AsiaTable,
self.AfricaTable,
self.LatinAmericaTable,
self.NorthAmericaTable, nil];
NSDictionary *dict = [NSDictionary dictionaryWithObject:array forKey:#"country"];

NSDictionary *countries =
[NSDictionary dictionaryWithObjectsAndKeys:
EuropeTable, #"You",
AsiaTable, #"Need To",
AfricaTable, #"Provide",
LatinAmericaTable #"More",
NorthAmericaTable, #"Details",
nil];
Without more details, I'm not sure any answer will help you much :)

NSMutableDictionary dict = [NSMutableDictionary dictionary];
[dict addObject:self.EuropeTable forKey:#"EuropeTable"];
etc...

Related

Filter NSArray of Object with NSArray of NSNumber with NSPredicate

i'm trying to filer an array of this Object:
MyObject.h
#interface MyObject : NSObject
#property (assign) int id_object;
#property (nonatomic, strong) NSString *name;
#end
So for example in my firstArray i have some MyObject:
MyObject *ob1 = [[MyObject alloc] init];
[ob1 setId_Object:1;
[ob1 setName:#"Carl"];
MyObject *ob2 = [[MyObject alloc] init];
[ob2 setId_Object:2;
[ob2 setName:#"Carl"];
MyObject *ob3 = [[MyObject alloc] init];
[ob3 setId_Object:3;
[ob3 setName:#"Carl"];
NSArray *firstArray = [[NSArray alloc] initWithObjects:ob1,ob2,ob3,nil];
then i want filter this array with NSPredicate with an array of NSNumber, like this:
NSArray *secondArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],nil];
i have tried this:
NSArray *filteredObject = [firstArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"id_object IN %#",secondArray]];
but the filteredObject is empty, how i can do?
Seems to be your test case that is wrong. I don't think your setId_object does what you think it does when calling it with an NSNumber;
[ob1 setId_object:[NSNumber numberWithInt:1]];
NSLog(#"%d", ob1.id_object);
> 391
[ob1 setId_object:1];
NSLog(#"%d", ob1.id_object);
> 1
Replacing the initializations makes your test case run ok.
EDIT: My test case, cut'n'pasted;
MyObject *ob1 = [[MyObject alloc] init]; ob1.id_object = 1; [ob1 setName:#"Carl"];
MyObject *ob2 = [[MyObject alloc] init]; ob2.id_object = 2; [ob2 setName:#"Carl"];
MyObject *ob3 = [[MyObject alloc] init]; ob3.id_object = 3; [ob3 setName:#"Carl"];
NSArray *firstArray = [[NSArray alloc] initWithObjects:ob1,ob2,ob3,nil];
NSArray *secondArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],nil];
NSArray *filteredObject = [firstArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"id_object IN %#",secondArray]];
NSLog(#"%#", filteredObject);
>>> <MyObject: 0x100110350> {1,"Carl"}
>>> <MyObject: 0x1001104b0> {2,"Carl"}
once check this one,
NSArray *filteredObject = [questionSections filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.Carl contains %# OR self.Carl contains %# ",#"1",#"2"]];
EX:-
NSArray* questionSections=[[NSArray alloc] initWithObjects:[[NSDictionary alloc] initWithObjectsAndKeys:
#"List 3 rooms in your house?",#"name",#"301q.png",#"questionpicture",nil],[[NSDictionary alloc] initWithObjectsAndKeys:#"Name a commonbird?",#"name",#"202q.png",#"questionpicture",nil], nil];
NSArray *filteredObject = [questionSections filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.questionpicture contains %# OR self.questionpicture contains %# ",#"2",#"3"]];
NSLog(#"%#",filteredObject);
O/P:--
(
{
name = "List 3 rooms in your house?";
questionpicture = "301q.png";
},
{
name = "Name a common bird?";
questionpicture = "202q.png";
}
)
try replace
#property (nonatomic, copy) NSNumber* id_object;
#property (nonatomic, copy) NSString *name;
instead of
#property (assign) int id_object;
#property (nonatomic, strong) NSString *name;

Filter NSArray of custom objects

I have a NSArray of Contact objects, we can call it contacts. Contact is a superclass, FacebookGroup and Individual are subclasses of Contact. FacebookGroup has a property called individuals which is a set of Individual objects.
I also have a NSArray of NSString objects, we can call it userIDs.
What I want to do is create a new NSArray from the existing contacts array that match the userIDs in userIDs.
So if contacts has 3 Contact objects with userID 1,2 and 3. And my userIDs has a NSString object 3. Then I want the resulting array to contain Contact which equals userID 3.
Contact.h
Contact : NSObject
FacebookGroup.h
FacebookGroup : Contact
#property (nonatomic, strong) NSSet *individuals;
Individual.h
Individual : Contact
#property (nonatomic, strong) NSString *userID;
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"userId = %#", myContact.userId];
NSArray *filteredArray = [contacts filteredArrayUsingPredicate:predicate];
Is this what you are looking for?
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"userID IN %#", userIDs];
NSArray *filtered = [contacts filteredArrayUsingPredicate:predicate];
i'm expecting you want like this once see this one,
NSMutableArray *names = [NSMutableArray arrayWithObjects:#"one", #"two", #"three", #"four", nil];
NSMutableArray *ids = [NSMutableArray arrayWithObjects:#"1", #"2", #"2", #"3", nil];
NSMutableArray *array=[[NSMutableArray alloc]init];
for(int i=0;i<[ids count];i++){
if([[ids objectAtIndex:i] isEqualToString:#"2"])
[array addObject:[names objectAtIndex:i]];
}
NSLog(#"%#",array);
O/P:-
(
two,
three
)

Is there a way to rearrange the contents of a list of arrays?

Is there a way to rearrange an array of objects such that the contents are rearranged such that all the xth elements are grouped in another array? I'm not sure the best way to explain this so I'll illustrate with an example:
theArray has 5 properties: number, color, size, height, quality. There are 50 instances of this array, each with different properties.
I want to rearrange this so there are 5 arrays with 50 items inside each array.
The way I would do it is:
NSMutableArray *innerTemp = [[NSMutableArray alloc] initWithCapacity:49];
NSMutableArray *outerTemp = [[NSMutableArray alloc] initWithCapacity:4];
for (int i = 0 ; i < [theArray count]; i++){
[innerTemp addObject:[[theArray objectAtIndex:i] number]];
[innerTemp addObject:[[theArray objectAtIndex:i] color]];
[innerTemp addObject:[[theArray objectAtIndex:i] height]];
[innerTemp addObject:[[theArray objectAtIndex:i] age]];
[innerTemp addObject:[[theArray objectAtIndex:i] quality]];
[outerTemp addObject:temp];
}
So I guess my question is, is there some easier way/more efficient to do this?
Just use KVC (Key Value Coding):
Assuming your class looks something like this:
#interface InstanceClass : NSObject
// IIRC properties need to be objects (hence the NSValue around NSSize), there is no auto-wrapping.
#property (readwrite, strong, nonatomic) NSNumber *number;
#property (readwrite, strong, nonatomic) NSColor *color;
#property (readwrite, strong, nonatomic) NSValue *sizeValue;
#property (readwrite, strong, nonatomic) NSNumber *height;
#property (readwrite, strong, nonatomic) NSNumber *quality;
#end
and you have an array like
NSArray *instances = #[instance1, instance2, instance3, instance4, ...];
then this would be all you need:
NSArray *numbers = [instances valueForKey:#"number"]; // array of numbers.
NSArray *colors = [instances valueForKey:#"color"]; // array of colors.
NSArray *sizes = [instances valueForKey:#"size"]; // array of sizes.
NSArray *heights = [instances valueForKey:#"height"]; // array of heights.
NSArray *qualities = [instances valueForKey:#"quality"]; // array of qualities.
This will do the trick.
Assuming your items in the instances array are numbered from 1..n a call to [instances valueForKey:#"number"] would then return an NSArray like #[#1, #2, #3, #4, ...].

Sort NSArray with custom objects

In my Xcode project I have the following classes:
Address
#interface LDAddress : NSObject{
NSString *street;
NSString *zip;
NSString *city;
float latitude;
float longitude;
}
#property (nonatomic, retain) NSString *street;
#property (nonatomic, retain) NSString *zip;
#property (nonatomic, retain) NSString *city;
#property (readwrite, assign, nonatomic) float latitude;
#property (readwrite, assign, nonatomic) float longitude;
#end
Location
#interface LDLocation : NSObject{
int locationId;
NSString *name;
LDAddress *address;
}
#property (readwrite, assign, nonatomic) int locationId;
#property (nonatomic, retain) LDAddress *address;
#property (nonatomic, retain) NSString *name;
#end
In an subclass of UITableViewController, there is a NSArray containing a lot of unsorted objects of LDLocations. Now, I want to sort the NSArray's objects ascending based on the property city of a LDAddress.
How can I sort the array using NSSortDescriptor?
I tried the following, but the app dumps when sorting the array.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"Address.city" ascending:YES];
[_locations sortedArrayUsingDescriptors:#[sortDescriptor]];
Try making the first key lowercase.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"address.city" ascending:YES];
You can also sort an array with blocks:
NSArray *sortedLocations = [_locations sortedArrayUsingComparator: ^(LDAddress *a1, LDAddress *a2) {
return [a1.city compare:a2.city];
}];
This will allow to sort with multiple types. Like we need to sort the Movie based on time and if the time is equal then need to sort by name.
NSArray *sortedArray = [childrenArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSNumber *first = [NSNumber numberWithLong:[(Movie*)a timeInMillis]];
NSNumber *second = [NSNumber numberWithLong:[(Movie*)b timeInMillis]];
NSComparisonResult result = [first compare:second];
if(result == NSOrderedSame){
result = [((NSString*)[(Movie*)a name] ) compare:((NSString*)[(Movie*)b name])];
}
return result;
}];
-(NSArray*)sortedWidgetList:(NSArray*)widgetList
{
NSSortDescriptor *firstDescriptor = [[NSSortDescriptor alloc] initWithKey:#"itemNum" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:firstDescriptor, nil];
NSArray *sortedArray = [widgetList sortedArrayUsingDescriptors:sortDescriptors];
return sortedArray;
}

How do I alphabetically sort a custom object field within a NSMutable Array?

I have a custom object like:
#import <Foundation/Foundation.h>
#interface Store : NSObject{
NSString *name;
NSString *address;
}
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSString *address;
#end
I have an array of NSMutableArray (storeArray) containing Store objects:
store1 = [[Store alloc] init];
store1.name = #"Walmart";
store1.address = #"walmart address here..";
store2 = [[Store alloc] init];
store2.name = #"Target";
store2.address = #"Target address here..";
store3 = [[Store alloc] init];
store3.name = #"Apple Store";
store3.address = #"Apple store address here..";
//add stores to array
storeArray = [[NSMutableArray alloc] init];
[storeArray addObject:store1];
[storeArray addObject:store2];
[storeArray addObject:store3];
My question is how can I sort the array by the store name? I know I can sort an array alphabetically by using this line:
[nameOfArray sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
How can I apply this to the store name of my Store class?
NSSortDescriptor *sortDescriptor =
[NSSortDescriptor sortDescriptorWithKey:#"name"
ascending:YES
selector:#selector(caseInsensitiveCompare:)];
[nameOfArray sortedArrayUsingDescriptors:#[sortDescriptor]];
Related documentation:
[NSArray sortedArrayUsingDescriptors:]
NSSortDescriptor
Regexident's answer is based on NSArrays, the corresponding in-place sorting for NSMutableArray would be -sortUsingDescriptors:
[storeArray sortUsingDescriptors:
[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"name"
ascending:YES
selector:#selector(caseInsensitiveCompare:)]]];
Now storeArray it-self will be sorted.