我在我的服務器上安裝了一個plist,用於在註釋信息中填充我的地圖。 當我從服務器讀取所有工作& plist文件的副本創建到文檔路徑。 我試圖在沒有互聯網連接的情況下閱讀我的plist文檔路徑,但它不起作用。 如果我在我的代碼中寫入,當沒有互聯網 - 它的工作,但從文檔 - 沒有。 (我改變了我的保管箱中plist文件的路徑) 我的代碼有什麼問題? 感謝您的高級幫助!從文檔中讀取plist
- (void) showMap
{
storsInfosArr = [[NSArray alloc]initWithContentsOfURL:
[NSURL URLWithString:@"http://dl.dropbox.com/u/4082823/AppsFiles/test.plist"]];
NSString *error;
NSString *rootPath =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"test.plist"];
NSArray *tmpArr = [NSArray arrayWithArray:storsInfosArr];
NSData *tmpData = [NSPropertyListSerialization dataFromPropertyList:tmpArr format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(tmpData)
{
[tmpData writeToFile:plistPath atomically:YES];
}
else
{
NSLog(@"%@",error);
}
//Check if the array loaded from server is not complete, load from docs:
if(tmpArr.count == 0)
{
NSString *rootPath =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"stores.plist"];
NSLog(@"docsPlistPath = %@",plistPath);
// Build the array from the plist
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
self.storsInfosArr = tmpArray;
}
for (NSDictionary *currStoreDict in storsInfosArr)
{
StoreAnnotation *stAnnotation = [[StoreAnnotation alloc] initWithDictionary:currStoreDict];
[myMapView addAnnotation:stAnnotation];
}
myMapView.showsUserLocation = YES;
CLLocationCoordinate2D tmpCoord2d = {31.48489338689016, 35.5517578125};
MKUserLocation *userLocation = myMapView.userLocation;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate,390000.0, 390000.0);
[myMapView setRegion:region animated:NO];
myMapView.mapType = MKMapTypeStandard;
self.myMapView.centerCoordinate = tmpCoord2d;
}
非常感謝..但它給了我一個錯誤self.dataDictonary = tempDict;什麼是dataDictionary?我沒有字典變量,如果這是什麼.. – 2012-03-05 16:31:20
dataDicitionary是變量用於訪問plist的根元素。根元素允許訪問plist內的剩餘值。 – Ram 2012-03-07 18:16:53