I want to know that remove blank index from this array.please see the code
array=( {
email = "regor#gmail.com";
fullname = regor;
photourl = "/download/userphoto/b239eb04-338d-4e93-9387-e0234850edfe.jpg";
userid = 56b09ea9088e7c6c118c9baa;
username = regor;
},
{
}
)
Instead of removing the blank index, You can store the first object into a separate dictionary.
Simply do
NSDictionary *dict=array[0];
Or If you want to remove the null from an array, try
[array removeObjectIdenticalTo:[NSNull null]];
Related
I have this dictionary in objective c:
{
awayScore = 2;
awayTeam = "Kode IF";
events = (
{
eventType = "01:00";
name = "01:00";
},
{
eventType = "01:00";
name = "01:00";
}
);
homeScore = 1;
homeTeam = "Partille IF FK ";
time = "01:00";
}
Now for populating a tableview i would need to know the size of "events" items so i can return the number of rows. Also how could i iterate then through them? (now I'm thinking when I'm going to use the values to populate the cells)
so 1) How can i get the size of it
and 2) How can i iterate through them and get the different values in uitableview
I know how to iterate through ALL THE KEYS, but since i don't know how to just loop through a specific key that has many items i can't show you what i have done so far
Thanks!!
Your JSon events object should be a JSon Array, so you can fast enumerate through it.
`
NSArray *events = jsonDictonary[#"events"];
for (NSDictonary *event in events) {
//parse each event
}
Where JsonDictionary is your entire Json object.
submittedTestArray = [{
scheduledTestId = "F7F15169-2FB6-4E47-A971-BAAD40D152AB";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "37F20871-FB3A-47AA-B254-9E5409C5E4C4";
}, {
scheduledTestId = "440947DD-0A01-4DB8-8DD5-CCFA8F852FD3";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "681689DC-B35C-491C-A737-AB19D9116FD9";
}, {
scheduledTestId = "440947DD-0A01-4DB8-8DD5-CCFA8F852FD3";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "681689DC-B35C-491C-A737-AB19D9116FD9";
}, {
scheduledTestId = "440947DD-0A01-4DB8-8DD5-CCFA8F852FD3";
studentId = "e18ead0a-d0fc-4e15-96ee-ecbd32880d97";
submittedTestId = "A46965C6-14B1-401E-97C5-AD5BADCC02EE";
}]
I want all the NSDictionary objects uniquely based on scheduledTestId
If i will consider above example then it will give me two NSDictionary objects.
Temporarily i am using following code.
NSMutableArray *uniqueKeys = [NSMutableArray arrayWithArray:[submittedTestArray valueForKeyPath:#"#distinctUnionOfObjects.scheduledTestId"]];
NSMutableArray *submittedTestUniqueArray = [[NSMutableArray alloc] initWithCapacity:uniqueKeys.count];
for (SubmittedTest *submittedTest in submittedTestArray) {
if ([uniqueKeys containsObject:submittedTest.scheduledTestId ]) {
[uniqueKeys removeObject:submittedTest.scheduledTestId];
[submittedTestUniqueArray addObject:submittedTest];
}
}
submittedTestUniqueArray is contain my desired output i.e.
[{
scheduledTestId = "F7F15169-2FB6-4E47-A971-BAAD40D152AB";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "37F20871-FB3A-47AA-B254-9E5409C5E4C4";
}, {
scheduledTestId = "440947DD-0A01-4DB8-8DD5-CCFA8F852FD3";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "681689DC-B35C-491C-A737-AB19D9116FD9";
}]
Instead of for-in loop, go with a for loop and after iterating each time
if ([uniqueKeys containsObject:submittedTest.scheduledTestId ]) {
[uniqueKeys removeObject:submittedTest.scheduledTestId];
}
else
{
[submittedTestArray removeObjectAtIndex:currentIndex];
}
I dont know what I am writing is up-to the mark but still I am daring to answer.
Ideally what you have tried is perfect. Coz to remove duplicate records from the dictionary, there is a need to have a bunch of records, in this case
an array named uniqueKeys with scheduledTestId values, to compare each dictionary element with all other elements for duplication.
And this is what exactly you have done.
The only improvements I can suggest in this code is instead of NSMutableArray *uniqueKeys
change it to NSArray *uniqueKeys; and after finishing the comparison & collecting the dictionary objects with the unique records, flush the array elements instead of removing them one by one.
Coz having 2 mutable arrays, one having simply unique keys & other having dictionary objects with unique records doesn't make any sense. Of course I presume that you dont need an array with only unique keys. What you need is dictionary not an array, right?
So in short I dont find any performance improvements in your code.
#ALL: Please let me know if there are any improvements in my technical knowledge.
CHEERS HAPPY CODING
The AddressBook Framework offers a great method for initializing an ABPerson with a vCard, by using the initWithVCardRepresentation: method.
What I want to do is update a contact with a certain vCard. I can't use initWithVCardRepresentation: because this will give me a new ABPerson object with a new uniqueId and I want to keep the uniqueId in between these changes.
What's an easy way to go about doing something like this?
Thanks!
initWithVCardRepresentation is still the slickest way to turn your vCard into an ABPerson.
Just use the results of it to find the matching person in your Address Book and then iterate over the vCard properties, placing them into the existing record. The save at the end will harden your changes.
The following example assumes that the unique "keys" will be last-name, first-name. You can modify the search element if you want to include companies where no name is listed or whatever, or you could change the iteration scheme by getting [AddressBook people], and then iterating over the people and using only those records where the key-value pairs match to your satisfaction.
- (void)initOrUpdateVCardData:(NSData*)newVCardData {
ABPerson* newVCard = [[ABPerson alloc] initWithVCardRepresentation:newVCardData];
ABSearchEleemnt* lastNameSearchElement
= [ABPerson searchElementForProperty:kABLastNameProperty
label:nil
key:nil
value:[newVCard valueForProperty:kABLastNameProperty]
comparison:kABEqualCaseInsensitive];
ABSearchEleemnt* firstNameSearchElement
= [ABPerson searchElementForProperty:kABFirstNameProperty
label:nil
key:nil
value:[newVCard valueForProperty:kABFirstNameProperty]
comparison:kABEqualCaseInsensitive];
NSArray* searchElements
= [NSArray arrayWithObjects:lastNameSearchElement, firstNameSearchElement, nil];
ABSearchElement* searchCriteria
= [ABSearchElement searchElementForConjunction:kABSearchAnd children:searchElements];
AddressBook* myAddressBook = [AddressBook sharedAddressBook];
NSArray* matchingPersons = [myAddressBook recordsMatchingSearchElement:searchCriteria];
if (matchingPersons.count == 0)
{
[myAddressBook addRecord:newVCard];
}
else if (matchingPersons.count > 1)
{
// decide how to handle error yourself here: return, or resolve conflict, or whatever
}
else
{
ABRecord* existingPerson = matchingPersons.lastObject;
for (NSString* property in [ABPerson properties]) // i.e. *all* potential properties
{
// if the property doesn't exist in the address book, value will be nil
id value = [newVCard valueForProperty:property];
if (value)
{
NSError* error;
if (![existingPerson setValue:value forProperty:property error:&error] || error)
// handle error
}
}
// newVCard with it's new unique-id will now be thrown away
}
[myAddressBook save];
}
In my iPhone app.
I am copying the one mutable array(with dictionary) to another.
It is like
resultsToDisplay = [[[NSMutableArray alloc]initWithArray:resultsPassed]mutableCopy];
2012-06-21 17:07:07.441 AllinoneCalc[3344:f803] Results To Display (
{
lbl = "Monthly EMI";
result = "75.51";
}
)
2012-06-21 17:07:08.224 AllinoneCalc[3344:f803] Results Passed (
{
lbl = "Monthly EMI";
result = "75.51";
}
)
Then I am modifying one of them .
[[resultsToDisplay objectAtIndex:i] setValue:[NSString stringWithFormat:#"%.2f",[[[resultsPassed objectAtIndex:i] valueForKey:#"result"] floatValue]] forKey:#"result"];
But what is happening that both are getting edited.
2012-06-21 17:07:08.703 AllinoneCalc[3344:f803] Results Passed (
{
lbl = "Monthly EMI";
result = "75.00";
}
)
2012-06-21 17:07:08.705 AllinoneCalc[3344:f803] Results To Display (
{
lbl = "Monthly EMI";
result = "75.00";
}
)
They both are referring to the same copy.
How to solve this. I want to modify only one array.
You are only copying the array of object pointers, not the object themself. See this article on deep copying.
an array (or mutable array) is just a list of objects, and you are modifing one of the objects referred in one of your array. the second array is still pointing to the same object, so it's normal that it changes too...
you are copying the array: meaning you're copying the list of objects, not copying all the objects in list.
I have a NSDictionary, which looks like:
0 = {
content = Test;
date = "2012-02-02 18:36:46 +0000";
};
1 = {
content = "#### da ####";
date = "2012-02-02 18:36:46 +0000";
};
2 = {
content = dfdffdfddfdffddf;
date = "2012-02-03 20:30:31 +0000";
};
But if I delete the second key (number 1 in the example) the dictionary ends up like:
0 = {
content = Test;
date = "2012-02-02 18:36:46 +0000";
};
2 = {
content = dfdffdfddfdffddf;
date = "2012-02-03 20:30:31 +0000";
};
How could I reorganizate them so it maintains the order (like 0, 1, 2, 3) when I need to?
NSDictionaries are unordered, meaning that you cannot control the order of keys and they may change randomly or in undocumented ways between iOS versions.
You have a few options:
Maintain your own, separate NSArray of keys, and use that to index your dictionary.
Use this OrderedDictionary class that I wrote, which basically does that but wraps it all up in an NSDictionary subclass so you don't have to do any extra work:
http://charcoaldesign.co.uk/source/cocoa#ordered-dictionary
If your dictionary keys are numbers (or if they are alphabetical, or something else that's easy to sort automatically), you could just sort the keys whenever you need to display them in order using:
NSArray *keys = [dict allKeys];
keys = [keys sortedArrayUsingSelector:#selector(compare:)];
//now loop over keys array instead of directly over dictionary