kamalbhai
kamalbhai

Reputation: 520

Add Unique Entries Into An Array - Xcode/Objective-C

I have the following text in a .txt file ::

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
            <key>aa</key>
            <string>2012-07-19 11:16:00</string>
    </dict>
    <dict>
            <key>bb</key>
            <string>2012-07-19 11:16:02</string>
    </dict>
    <dict>
            <key>cc</key>
            <string>2012-07-19 11:16:05</string>
    </dict>
    <dict>
            <key>dd</key>
            <string>2012-07-19 11:16:07</string>
    </dict>
    <dict>
            <key>aa</key>
            <string>2012-07-19 11:16:10</string>
    </dict>
    <dict>
            <key>bb</key>
            <string>2012-07-19 11:16:13</string>
    </dict>

I put these contents into an array by using the following line of code ::

self.kkkk = [NSMutableArray array];

NSString *Dir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [Dir stringByAppendingPathComponent:@"abcd.txt"];

NSArray *array = [NSArray arrayWithContentsOfFile:filePath ];
kkkk = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array) {
    [kkkk addObjectsFromArray:[dict allKeys]];

}

while adding the contents I wish to put only the unique values for aa, bb, cc and dd. For this, I want to put their one single entry in the array kkkk alongwith their latest timestamp.

How can I do that ?? Especially the comparing of two timestamp .. can someone help me out ?? Thanks and Regards.

Upvotes: 0

Views: 806

Answers (1)

V-Xtreme
V-Xtreme

Reputation: 7333

First get all the object from dictionary and then check following

for (NSDictionary *dict in array) 
{
    if(![kkkk containsObject:objectFromDictionary])
    {
       [kkkk addObjectsFromArray:[dict allKeys]];
    }
}

this may work but you should add object one by one instead of adding all keys.

Upvotes: 1

Related Questions