从文件中读取plist

时间:2012-02-18 13:17:59

标签: load plist documents

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

2 个答案:

答案 0 :(得分:1)

以下代码可帮助您阅读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 
}

答案 1 :(得分:-1)

在访问其他对象之前,需要在字典变量中加载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>

首先在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


}