Array with array inside - ios

I need to create an array with personal name inside,and on this name,another array with personal information.Basically an array with another array inside divided by name.
Es:Array[luca [born:x age:y lives:z] marco[born:x age:y lives:z]......}
How can i do that?

Very simple, use modern Objective-C literals.
NSDictionary *luca = #{#"name" : #"luca",
#"born" : #(1997),
#"lives" : #(5)};
NSDictionary *marc = #{#"name" : #"marc",
#"born" : #(1998),
#"lives" : #(2)};
NSArray *people = #[luca, marc];

Your brief description indicates that you might want a dictionary of dictionaries. However, what you want here is an array of dictionaries or an array of objects.
var people = [ ["name": "Luca", "born": x, "age": y, "lives": z],
... ]
or
struct Person {
var name:String
var born:Int16
var age:Int16
var lives:Int16
}
var array = [Person(name: "Luca", born: x, age: y, lives: z),
...]

To add onto these answers you can also easily create a mutable NSMutableDictionary or NSMutableArray using literal syntax as follows:
NSMutableDictionary *dict = [#{#"asdf":#"asdf"} mutableCopy];
NSMutableArray *arr = [#[#"asdf"] mutableCopy];

Related

How to filter one array of strings based on the contains of a second array of strings

Solved, i just can't select my own answer
I have an array that gets loaded from JSON into an NSMutableArray. One of the entries, AllowedTo, has a value of New York, Florida
I also have a second array that comes in from a separate JSON to a new NSMutableArray and using this array, I populate a UITableView:
Using the first array, I want to hide any cell that doesn't match in the second array. For example, the end result will (using the image posted above) only have 3 cells (GRAND TOTAL, NEW YORK, FLORIDA) and the rest (JUNIORS, FOODNATION, etc...) will be hidden.
Here's an example of how you could do it using the filter function.
let arrayOne = ["one", "two", "three", "four", "five"]
let arrayTwo = ["one", "two", "three"]
let filteredArray = arrayOne.filter({ arrayTwo.contains($0)})
print("filteredArray: \(filteredArray)")
I would then create a function for this so you can call it as needed without altering your original arrays.
func filterDataSource(arrayOne: [String], arrayTwo: [String]) -> [String] {
let filteredArray = arrayOne.filter({ arrayTwo.contains($0)})
return filteredArray
}
EDIT
The same functionality in Objective-C, there are more elegant ways of doing it, but I am a bit rusty :)
NSArray *arrayOne = #[#"one", #"two", #"three", #"four", #"five"];
NSArray *arrayTwo = #[#"one", #"two"];
NSMutableArray *filteredArray = [[NSMutableArray alloc] initWithCapacity:5];
for (NSString *string in arrayOne) {
for (NSString *filterString in arrayTwo) {
if ([string isEqualToString:filterString]) {
[filteredArray addObject:string];
continue;
}
}
}
You could also use sets:
NSMutableSet *dataToShow = [NSMutableSet setWithArray:yourFirstArray];
NSSet *other = [NSSet setWithArray:yourSecondArray];
[dataToShow intersectSet:other];
Now "dataToShow" has only the items that exist in both of your arrays.
EDIT
For example:
Array1 has "1", "2", 3", "4"
Array2 has "1", "4", "5"
Set1 will have Array1's elements, but probably different order: "2", "4", "3", "1
Same for Set2: "4", 1", "5"
The intersected set, the "dateToShow", would then have elements "1" and "4" because those are the only ones present in both sets.
However, the order can't be guaranteed, so if that's important to you...I suggest [dataToShow.allObjects sortedArrayUsingComparator....];
so, i was looking at this the wrong way. my intent was to parse a JSON in to an array and then strip out what i dont need. when it was much more simpler to just fill the array with what i want to show. at the end of my JSON parser, i added this
if ([AllowedTo containsString:newEstNet.EntityName]) {
NSLog(#"%# displayed", newEstNet.EntityName);
[listOfEstNetGrand addObject:newEstNet];
} else {
NSLog(#"%# not displayed", newEstNet.EntityName);
// dont add the object to the array
}

How to get Array inside fields from JSON Structure in ios

Hi i am beginner in ios and in my project i am integrating services with my app but i am struggling to integrate some service fields with my app
Please see below JSON structure there i want to get all "languages" Array inside elements but i am not understand how to get those fields please help me some one
Json stucture:-
- loans: [

 - {
id: 983381,
name: "America",
description: 
 {
languages: 
 [
"English"
]
},
},
- {
id: 983382,
name: "Jarmani",
description: 
 {
languages: 
 [
"Jarman"
]
},
},
- {
id: 983383,
name: "Rasya",
description: 
 {
languages: 
 [
"Rasya"
]
},
},
]
my code:-
NSMutableArray * array1 = [mainDictyionary objectForKey:#"loans"];
for (NSDictionary * obj in array1) {
MainArray = [obj objectForKey:#"languages"];
}
NSLog(#"so finally array values are %#",MainArray);
You want to
Declare MainArray as a NSMutableArray: MainArray = [[NSMutableArray alloc] init];
Now you will be adding the "languages" NSArrays to a 'mother-array', I'm guessing that is not what you want
So you want to iterate over the [obj objectForKey:#"languages"] array and call addObject on the MainArray for every NSString value you'll encounter in the [obj objectForKey:#"languages"]-iteration.
You'll end up with a mutable NSArray that has all the languages in it, as NSStrings
Actually you are getting the value upto languages and not upto "english" or anyother. And also you are assigning the values to MainArray, so it will not add all the values it will add last value of the loop.
NSMutableArray * array1 = [mainDictyionary objectForKey:#"loans"];
NSMutableArray *MainArray = [NSMutableArray alloc] init];
for (NSDictionary * obj in array1) {
NSDictionary *descriptionDictionary = [obj objectForKey:#"description"];
[MainArray addObject:[obj objectForKey:#"languages"]];
}
NSLog(#"so finally array values are %#", MainArray);

Objective-c NSMutableDictionary set with an array keep empty

I'm new here, but I use to read this site when I need something, but today, I can't find an answer to my question.
I'll try to explain my problem with enough details.
I need to add an array into a NSMutableDictionary at a specific key. The key added into it is correctly up, but my dictionary value keep empty. Here is my code :
dictionarySection = [[NSMutableDictionary alloc] initWithObjects:arraySectionValues forKeys:arraySectionKeys];
dictionaryClip = [[NSMutableDictionary alloc] initWithCapacity:[arraySectionKeys count]];
NSArray *tabSection = [dictionarySection allKeys];
id key,value;
for (int j=0; j<tabSection.count; j++)
{
array = [NSMutableArray array];
key = [tabSection objectAtIndex: j];
value = [dictionarySection objectForKey: key];
//NSLog (#"Key: %# for value: %#", key, value);
for (SMXMLElement *clip in [books childrenNamed:#"clip"]) {
if([[clip valueWithPath:#"categorie"] isEqualToString:value]){
[array addObject:[clip valueWithPath:#"titre"]];
}
}
NSLog(#"Test array %#",array);
[dictionaryClip setObject:array forKey:key];
[array removeAllObjects];
NSLog(#"Test dictionary %#",dictionaryClip);
}
Here the NSLog result :
2015-07-15 14:34:48.272 test[15533:390301] Test array (
"CDS : ITV Philippe Dunoyer",
"FLASH INFO NCI : crise des banques",
"Les Roussettes sont-elles dangereuses ?",
"Flash infos banques gr\U00e8ve",
"CDS : ITV Paul Langevin",
"CDS : ITV Valls",
"CDS : ITV Victor Tutugoro",
"CDS : ITV Roch Wamytan",
"NCGLAN 20",
"Flash Info : dispositif anti-d\U00e9linquance"
)
2015-07-15 14:34:48.273 test[15533:390301] key : 0
2015-07-15 14:34:48.273 test[15533:390301] Test dictionary {
0 = (
);
}
As we can see, the array is filled, the dictionary's key is correct, but the array isn't into my dictionary.
How may I suppose to fill my dictionary with this array?
Thanks a lot guy(s) for answer(s) :)
Ps : excuse my english :(
You are calling removeAllObjects: method for same instance of array which you are passing in dictionary so it objects are being removed in stored array. Try to pass that array's copy or a new instance of array with same objects.
Example:
[dictionaryClip setObject:[array copy] forKey:key];
In Objective-C arrays are reference types.
The method setObject:forKey: puts a pointer to the array into the dictionary, the array is not copied.
If you remove all objects from the array, they also disappear in the dictionary

Create sorted array from multi-dimensional NSDictionary's sub-dictionary values

I have an NSDictionary which contains sub NSDictionaries and I was wondering if it is possible to alphabetically sort the main dictionary by the value of the "name" keys of the sub dictionaries.
NSDictionary *student1 = #{#"first_name" : #"Scott", #"last_name" : #"Smith"};
NSDictionary *student2 = #{#"first_name" : #"John", #"last_name" : #"Donalson"};
NSDictionary *student3 = #{#"first_name" : #"Mary", #"last_name" : #"Patricia"};
NSDictionary *student4 = #{#"first_name" : #"Zack", #"last_name" : #"Elliot"};
NSDictionary *students = #{"001": student1, "002": student2, "003": student3, "004": student4};
I was hoping to be able to create an array or mutable array that only holds the keys but is sorted depending on the values of certain keys in the sub-dictionaries. Something like this:
last_name_array
["002", "004", "003", "001"];
first_name_array
["002", "003", "001", "004"];
Check out NSDictionary method keysSortedByValueUsingComparator:
You provide a comparator block that works on the values (your student dictionaries) and returns an array of keys.
https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/doc/uid/20000140-SW21

Add data from an array using its value for key to another Array

Hi i have two Arrays one being Parsed Data that has multiple entries at each cell (FolderName & ID) i want this data to be saved into another array like it is stored in the first array
FilteredData = [[NSMutableArray alloc]initWithArray:[ParsedData ValueForKey:#"FolderName"]];
SearchData = [[NSMutableArray alloc]initWithArray:FilteredData];
This is what i have been trying, The data Containing a dictionary at each entry is ParsedData, And the Array in which i have to copy these items is SearchData.
The Array (Parsed Data) Containing the dictionary looks like this
PARSED DATA (
{
CreatedBy = 1;
FolderName = Posteingang;
ID = 13000;
ParentID = 0;
},
{
CreatedBy = 1;
FolderName = Freigaben;
ID = 13001;
ParentID = 0;
},
From this Array I need to Get the FolderName And the ID and get them in the SearchData Array
SearchData (
Posteingang;
13000;
Freigaben;
13001;
)
I hope the question is clear now
check this one,
NSMutableArray *searchData = [NSMutableArray new];
for(NSDictionary *dict in ParsedDataArray){//here ParsedDataArray means PARSED DATA array name
NSDictionary *tempDict=[[NSDictionary alloc]initWithObjectsAndKeys:[dict ValueForKey:#"FolderName"],#"FolderName",[dict ValueForKey:#"ID"],#"ID" nil],
[searchData addObject:dict];
}
It seems like you misspelled the method name, it should be:
dictionaryWithObjectsAndKeys:
Notice the "s" after Object.
I want a dictionary inside an array which contains foldername and id
So you can get it using this:
NSMutableArray *searchData = [NSMutableArray new];
for(NSDictionary *dict in parsedDataArray){
NSDictionary *tempDict=#{#"FolderName":dict[#"FolderName"],
#"ID":dict[#"ID"]};
[searchData addObject:tempDict];
}
EDIT:
#[#"key:#"object"] is the new dictionary literal.
If you are using Xcode 4.3 or older need to do as:
NSDictionary *tempDict=#{#"FolderName":dict[#"FolderName"],
#"ID":dict[#"ID"]};
means,
NSDictionary *tempDict=[NSDictionary dictionaryWithObjectsAndKeys:[dict objectForKey:#"FolderName"], #"FolderName",[dict objectForKey:#"ID"],#"ID", nil];

Resources