0

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.

4
  • "I have the following text in a .txt file" - You might want to change it to a .plist. Commented Jul 19, 2012 at 6:00
  • No, because I want the extension to be .txt !! Commented Jul 19, 2012 at 6:04
  • But you want to add them to an array and making it a .plist file greatly eases the process. The top answer here (<stackoverflow.com/questions/6977015/…) does it in 4 lines of code... Also, your .txt file is in .plist format. Look at the second tag: "<!DOCTYPE plist..." Commented Jul 19, 2012 at 6:07
  • but I dont find any code where comparison of two timestamps has been done Commented Jul 19, 2012 at 6:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

what is objectFromDictionay ?? is it the same as dict ?
ya Dict is object of dictionary. If u parse this u can get all keys then by using above if condition u can put elements uniquely in array. I don't know why r u using dictionary here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.