views:

514

answers:

4

This is a follow up question on my first queries regarding retrieving data on plist. Right now i have manage to detect users touches made on my view with random image call out (thanks to phytonquick).

CGPoint currentTouchLocation = [currentTouch locationInView:self];

Im having trouble now on how to compare the value i got from the users touches made on the random image in the view to the one save inside the plist data with the same name as the random image the users touches. I know how to calculate the touch distance so i could adjust the hit points.

-(CGFloat) distanceBetween: (CGPoint) point1 and: (CGPoint)point2

   NSMutableDictionary *????? = [self loadDictionaryFromPList: @"?????"];
NSNumber *1stXCoordinate = [????? objectForKey:@"1stXCoordinate"];
NSNumber *1stYCoordinate = [????? objectForKey:@"1stYCoordinate"];
if (1stXCoordinate && 1stYCoordinate)
{
    CGPoint 1stTouchLocation = CGPointMake([lastXCoordinate floatValue], [lastYCoordinate floatValue]);
    CGFloat distanceBetweenTouches = [self distanceBetween: currentTouchLocation and: 1stTouchLocation];
    if (distanceBetweenTouches < 20)
    {
     // do something here 
        NSLog(@"You hit it.");
    }
}

Also I dont know on how should i construct my plist data. I've read several books with sample of it (like the "iphone 3 dev") but i cant figure it out specially when most of the samples i saw are for tableview purposes. This is how i layout my plist:

randImage <-- callout array
 p1.jpg <-- image represented by array
  tap1 <-- Array
   item 1 - x1 coordinate <-- Number
   item 2 - y1 coordinate <-- Number
   item 3 - x2 coordinate <-- Number
   item 4 - y2 coordinate <-- Number
   item 5 - x3 coordinate <-- Number
   item 6 - y3 coordinate <-- Number
  tap2 <-- Array
   item 1 - x1 coordinate <-- Number
   item 2 - y1 coordinate <-- Number
   item 3 - x2 coordinate <-- Number
   item 4 - y2 coordinate <-- Number
 p2.jpg <-- image represented by array
  tap1
   item 1....etc

Could someone please direct me to the right path on how should i do this. Thank you.

A: 

Look into the documentation for -initWithContentsOfFile:

You should be able to create dictionaries and arrays from plists, provided that they are standard.

In this case, you can call [[NSDictionary alloc] initWithContentsOfFile:@"path/to/plist"]; And then you can use -valueForKey: to get the data you need.

Jasarien
+2  A: 

Here are two methods to read and write values from a plist using an NSDictionary:

- (NSMutableDictionary*)dictionaryFromPlist {
    NSString *filePath = @"myPlist.plist";
    NSMutableDictionary* propertyListValues = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    return [propertyListValues autorelease];
}

- (BOOL)writeDictionaryToPlist:(NSDictionary*)plistDict{
    NSString *filePath = @"myPlist.plist";
    BOOL result = [plistDict writeToFile:filePath atomically:YES];
    return result;
}

and then in your code block somewhere:

// Read key from plist dictionary
NSDictionary *dict = [self dictionaryFromPlist];
NSString *valueToPrint = [dict objectForKey:@"Executable file"];
NSLog(@"valueToPrint: %@", valueToPrint);

// Write key to plist dictionary
NSString *key = @"Icon File";
NSString *value = @"appIcon.png";
[dict setValue:value forKey:key];

// Write new plist to file using dictionary
[self writeDictionaryToPlist:dict];
Brock Woolf
You've got some conventions wrong. The "get" prefix means that the result is passed by reference (ie, you pass in a pointer as a parameter, and the method fills the parameter). Additionally, you're returning an NSMutableDictionary with a retain count of +1, which you're leaking after reading the key from the plist dictionary. You should call it "mutableDictionaryFromPlist" and `return [propertyListValues autorelease];`
Dave DeLong
Yes C++ habits die hard some things still influence my coding in other languages. I'll pay you on the autorelease pickup though.
Brock Woolf
@Brock despite the missed conventions, it's still a solid answer. +1
Dave DeLong
A: 

Hi !

I'm beginner and I try to understand... HOW I can make a request to show all data (from plist) in SAME category (NOT ALL !) for exemple : show only the movie have : genre = Action

Please help me ... Thanks a lot ! Excuse my bad english !

Is it in this code I must make it or an other ???

  • (id)initWithLibraryName:(NSString *)libraryName { if (self = [super init]) { libraryPlist = libraryName; libraryContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:libraryPlist ofType:@"plist"]];
Fraise
A: 

HOW I can make a request to show from data plist only one categoy for exemple : show for the movie : category = Action

you can download the source for help me on : http://www.iphonedevcentral.com/wp-content/uploads/2009/08/MyDVDLibrary03.zip PLEASE SAY ME HOW I CAN DO IT !

I will want to know the exact CODE LINE and WHERE I must write it ! For see a the list of product are in Action.

THANKS VERY VERY MUCH Are you near France ? for say to you : THANKS, I can send a product of my country if you help me for my appli and maybe more...

Fraise