在开始之前,我正在以编程方式创建 plist
我将图像的路径名存储在 plist 中(以编程方式)。在保存时,我使用了 NSLog,下面是我所拥有的
2013-08-04 15:25:24.044 XXX[12595:13d03] inserting data is 5===http://www.mysite.com/faces/ProjectUploadFiles/hotDeals/mobile_2013-08-04-07-42-37.25_tstdddd.png
2013-08-04 15:25:24.057 XXX[12595:13d03] inserting data is 4===http://www.mysite.com/faces/ProjectUploadFiles/hotDeals/mobile_2013-08-04-07-18-20.673_iphone_2.jpg
2013-08-04 15:25:24.086 XXX[12595:13d03] inserting data is 2===http://www.mysite.com/faces/ProjectUploadFiles/hotDeals/mobile_2013-07-21-03-14-29.292_Spare-Parts-summer-Ad-hyundai.jpg
2013-08-04 15:25:24.087 XXX[12595:13d03] inserting data is 1===http://www.mysite.com/faces/ProjectUploadFiles/hotDeals/mobile_2013-07-21-03-11-55.395_horizon.jpg
2013-08-04 15:25:24.089 XXX[12595:13d03] inserting data is 6===http://www.mysite.com/faces/ProjectUploadFiles/hotDeals/mobile_2013-08-04-08-03-29.371_2010.jpg
输入数据的 ID 为 5,4,2,1,6。
现在,当我阅读这些数据时,我得到了以下信息。
2013-08-04 15:27:28.251 XXX[12595:13d03] fetching URL == http://www.mysite.com/faces/ProjectUploadFiles/hotDeals/mobile_2013-08-04-07-18-20.673_iphone_2.jpg
2013-08-04 15:27:28.252 XXX[12595:13d03] fetching URL == http://www.mysite.com/faces/ProjectUploadFiles/hotDeals/mobile_2013-08-04-07-42-37.25_tstdddd.png
2013-08-04 15:27:28.252 XXX[12595:13d03] fetching URL == http://www.mysite.com/faces/ProjectUploadFiles/hotDeals/mobile_2013-07-21-03-11-55.395_horizon.jpg
2013-08-04 15:27:28.252 XXX[12595:13d03] fetching URL == http://www.mysite.com/faces/ProjectUploadFiles/hotDeals/mobile_2013-08-04-08-03-29.371_2010.jpg
2013-08-04 15:27:28.252 XXX[12595:13d03] fetching URL == http://www.mysite.com/faces/ProjectUploadFiles/hotDeals/mobile_2013-07-21-03-14-29.292_Spare-Parts-summer-Ad-hyundai.jpg
意味着在获取数据时我得到了 4,5,1,6,2 的数据
当我双击 plist 时,我有 id 排序的数据,即我有数据为 1、2、4、5、6。
知道为什么 plist 提供随机数据吗?
下面是正在生成的 plist 的屏幕截图。
编辑 1
对于存储数据,以下是我拥有的代码。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Offers.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
if(![[NSFileManager defaultManager] removeItemAtPath:path error:&error])
{
//TODO: Handle/Log error
NSLog(@"files not deleted...");
} else {
NSLog(@"files deleted...");
}
if (![fileManager fileExistsAtPath: path])
{
path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"Offers.plist"] ];
}
NSMutableDictionary *data002 = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
if ([fileManager fileExistsAtPath: path])
{
data002 = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
}
else
{
// If the file doesn’t exist, create an empty dictionary
data002 = [[NSMutableDictionary alloc] init];
}
int count;
for (count = 0; count < (int)[news count]; count++)
{
[data002 setObject:[[news objectAtIndex:count] objectForKey:@"imagePath"] forKey:[[news objectAtIndex:count] objectForKey:@"id"]];
[data002 writeToFile:path atomically:YES];
}
[data002 release];
}
回答
我所做的是我使用 NSMutableArray 而不是 NSMutableDictionary 并且一切都运行良好。