关于如何从plist中和放数据的问题;应该如何布局 [英] A question on how to Get data from plist & how should it be layout

查看:189
本文介绍了关于如何从plist中和放数据的问题;应该如何布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对上的plist检索数据的查询首先跟进的问题。现在我得设法发现我与随机图像叫出(感谢phytonquick)视图中所做的用户触摸。

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];

林现在有关于如何比较我从欣赏到一个随机图像上所做的用户触摸得到的值的plist里面的数据保存使用相同的名称为随机图像的用户触摸的麻烦。我知道如何计算触摸距离,所以我可以调整点生命值。

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.");
    }
}

另外,我不知道我应该怎么构建我的plist数据。我读过几本书与它的样本(如iPhone 3开发),但我不能弄明白特别是当大多数我看到的样品是的tableview的目的。我这是怎么布置我的plist:

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.

推荐答案

下面两种方法使用一个NSDictionary一个plist中读取和写入值:

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;
}

,然后在code座的地方:

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];

这篇关于关于如何从plist中和放数据的问题;应该如何布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆