从文档中读取plist

发布于 2025-01-07 01:05:06 字数 2060 浏览 1 评论 0原文

我的服务器上有一个 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;
}

I have a plist on my server for populating my map with annotations information.
When i read it from the server everything working & a copy of the plist file is created to the documents path.
I'm trying to read my plist in the documents path when there is no internet connection but it doesn't work.
If i'm writing in my code to go to the bundle when there is no internet - its working but from documents - no.
(i have changed just for the question the path of the plist file in my dropbox)
What's wrong in my code?
Thanks for your help in advanced!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久而酒知 2025-01-14 01:05:06

您好,以下代码将帮助您从 NSDocumentDirectory 读取 plist:

if(netConnection==0)
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    if (!documentsDirectory) {
        NSLog(@"Documents directory not found!");
    }

    NSArray *myWords = [@"Data.plist" componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
    NSString *PlistFilePath = [documentsDirectory stringByAppendingPathComponent:[myWords lastObject]];

    NSLog(@"%@",PlistFilePath);

    NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:PlistFilePath];

    //Load Plist root element into Dictionary object Fist

    self.dataDict = tempDict;

    [tempDict release];

    //From DataDict retrieve the Array 
}

Hi following code will help you in reading plist from NSDocumentDirectory:

if(netConnection==0)
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    if (!documentsDirectory) {
        NSLog(@"Documents directory not found!");
    }

    NSArray *myWords = [@"Data.plist" componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
    NSString *PlistFilePath = [documentsDirectory stringByAppendingPathComponent:[myWords lastObject]];

    NSLog(@"%@",PlistFilePath);

    NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:PlistFilePath];

    //Load Plist root element into Dictionary object Fist

    self.dataDict = tempDict;

    [tempDict release];

    //From DataDict retrieve the Array 
}
柏拉图鍀咏恒 2025-01-14 01:05:06

在访问其他对象之前,需要将 plist 的根元素加载到字典变量中。

<plist version="1.0">
  <dict>
  <key>Levels</key> //this is Root Element

  <array>
  <dict>
 <key>Title</key><string>Screen Based Videos</string>
  </dict>

  <dict>
  <key>Title</key><string>Action Based Videos</string>
  </dict>

   </array>
   </dict>
   </plist>

首先在应用程序委托类中创建一个字典变量。

 .h AppDelClass

NSDictionary *DataDict;

@property(nonatomic,retain)  NSDictionary *DataDict;



.M AppDelClass

@sythesize DataDict;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

Check Network Connection using Rechability Class form Apple Doc

NSURL *XmlDataURL=[NSURL URLWithString:@"http://example.com/ideploy/testing/Data.plist"];


if(NetConnection==YES)
{


   theRequest = [NSURLRequest requestWithURL:XmlDataURL    cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    receivedData = [[NSMutableData alloc] initWithLength:0];
    Connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self  startImmediately:YES];

}

else
{


   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!documentsDirectory){
        NSLog(@"Documents directory not found!");
    }
    NSArray *myWords = [@"Data.plist" componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"`]];
NSString *PlistFilePath = `[documentsDirectory stringByAppendingPathComponent:[myWords lastObject]];

NSLog(@"%@",PlistFilePath);

NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:PlistFilePath];

//Load Plist root element into Dictionary object Fist

 self.dataDict = tempDict;

[tempDict release];

}

//Root Class Implementation
 .h file

NSArray *tableDataSource;

@property (nonatomic, retain) NSArray *tableDataSource;

.M file

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

     NSArray *tempArray = [[NSArray alloc] init];
    self.tableDataSource = tempArray;
   [tempArray release];

 RootClassAppDelegate *AppDelegate = (RootClassAppDelegate *)[[UIApplication  sharedApplication] delegate];

 self.tableDataSource = [AppDelegate.data objectForKey:@"Levels"];

     NSLog(@"%i",[tableDataSource count]);

// form the tableDataSource Array you can Access remaining elemements


}

Root element of plist needs to be Loaded in Dictionary Variable before Accessing the Other Objects.

<plist version="1.0">
  <dict>
  <key>Levels</key> //this is Root Element

  <array>
  <dict>
 <key>Title</key><string>Screen Based Videos</string>
  </dict>

  <dict>
  <key>Title</key><string>Action Based Videos</string>
  </dict>

   </array>
   </dict>
   </plist>

First Create a Dictionary Variable in App Delegate Class.

 .h AppDelClass

NSDictionary *DataDict;

@property(nonatomic,retain)  NSDictionary *DataDict;



.M AppDelClass

@sythesize DataDict;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

Check Network Connection using Rechability Class form Apple Doc

NSURL *XmlDataURL=[NSURL URLWithString:@"http://example.com/ideploy/testing/Data.plist"];


if(NetConnection==YES)
{


   theRequest = [NSURLRequest requestWithURL:XmlDataURL    cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    receivedData = [[NSMutableData alloc] initWithLength:0];
    Connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self  startImmediately:YES];

}

else
{


   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!documentsDirectory){
        NSLog(@"Documents directory not found!");
    }
    NSArray *myWords = [@"Data.plist" componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"`]];
NSString *PlistFilePath = `[documentsDirectory stringByAppendingPathComponent:[myWords lastObject]];

NSLog(@"%@",PlistFilePath);

NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:PlistFilePath];

//Load Plist root element into Dictionary object Fist

 self.dataDict = tempDict;

[tempDict release];

}

//Root Class Implementation
 .h file

NSArray *tableDataSource;

@property (nonatomic, retain) NSArray *tableDataSource;

.M file

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

     NSArray *tempArray = [[NSArray alloc] init];
    self.tableDataSource = tempArray;
   [tempArray release];

 RootClassAppDelegate *AppDelegate = (RootClassAppDelegate *)[[UIApplication  sharedApplication] delegate];

 self.tableDataSource = [AppDelegate.data objectForKey:@"Levels"];

     NSLog(@"%i",[tableDataSource count]);

// form the tableDataSource Array you can Access remaining elemements


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文