I am not sure what I am doing wrong here. I have a simple plist on my server.
I am using the ASIHTTPRequest framework, so I can get both the data object and a string.
Outputting: [[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding] works just fine and the output it the plist.
However, I cannot get this parsed into an NSArray. I have tried with: [[NSArray alloc] initWithContentsOfFile:[[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding]] but without any luck.
Thanks
If you create plist file that contain elements array theninitWithContentsOfFile: works fine. The argument to '-[NSArray initWithContentsOfFile:]' should be a file path and file should be stored locally. That means the app should have read access to the stored file.
If your server is giving you plist file content , you can store it locally and use initWithContentsOfFile: method create array from file content.
Regards
Devara Gudda
Related
A KML file I include in the project parses correctly whereas the same file downloaded from a server does not?!
I modified Apple's KML parsing example to parse a KML file. If I add the KML file directly into the Xcode project it parses correctly and displays it's polylines, annotations, etc. on the map as expected. But if I download the same KML file from my server, save it to disk and attempt to parse it, the parsing does not work. i.e. the parser does not find any elements to parse in the file so nothing is displayed on the map!
Firstly I have verified that the file downloads and saves to disk correctly. I checked the simulator's data (SimPholders is a handy tool for this) to get the file and compare it to the original one in case of corruption, no problem there, it downloads and saves correctly, no corruption.
I next checked the URL to the file I pass to the KML parser to ensure that it is correct and it was fine. I used NSFileManager fileExistsAtPath to determine this, the KML file is at the path I provide to the parser.
In the map view controller's viewDidLoad method I download the file from the server and pass it to the parser.
- (void)viewDidLoad
{
[super viewDidLoad];
[[KmlDownloader sharedKmlDownloader] downloadFileFrom:#"http://www.myserver.com/file.kml" isAsyncDownload:NO];
NSURL *kmlUrl = [[KmlDownloader sharedKmlDownloader] urlForKmlFile];
if (kmlUrl)
{
kmlParser = [[KMLParser alloc] initWithURL:kmlUrl];
[kmlParser parseKML];
}
// Add all of the MKOverlay objects parsed from the KML file to the map.
NSArray *overlays = [kmlParser overlays];
[map addOverlays:overlays];
// Add all of the MKAnnotation objects parsed from the KML file to the map.
NSArray *annotations = [kmlParser points];
[map addAnnotations:annotations];
// Some positioning code of the view port here
}
KmlDownloader as a reference can be found as a Gist here. Sorry the formatting was messing up too much to include inline, its a relatively small file though.
Any ideas why this may be happening? Many thanks.
Ok found the issue. It was to do with how I was accessing the file once downloaded. Once saved to disk I would send it's URL to the parser. The problem was I was using the incorrect NSURL method, I was originally using:
NSURL *fileUrl = [NSURL URLWithString:[self pathForFile:self.pathToKmlFileOnDisk]];
Whereas I should have been using:
NSURL *fileUrl = [[NSURL alloc] initFileURLWithPath:self.pathToKmlFileOnDisk];
Both are the same URL but the second one includes the "file:///" prefix. Once changed the parser would parse as expected.
I have an app that currently saves a NSMutableArray to a file in the documents directory on the iPad. This is saved by
[residentData writeToFile:filePath atomically:NO];
and is read back in by;
residentData = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
I need to write this file to iCloud, as the data needs to be available across several devices. I have tried the following to create the file in iCloud;
NSData *myFileData = [NSKeyedArchiver archivedDataWithRootObject:residentData];
[fileManager createFileAtPath:[icloudURL path] contents:myFileData attributes:nil];
Which does indeed create a file in the correct place. I am struggling though to understand how to read this file back into my NSMutableArray (residentData). I have tried;
residentData = [NSKeyedUnarchiver unarchiveObjectWithData:myFileData];
But this doesn't decode into the array correctly. Please can someone show me the error of my ways - I suspect a fundamental misunderstanding on my part of NSData is to blame, but any pointers would be welcome.
It finally clicked, my approach was wrong. I don't need to use any of the above. Once I have done the;
[fileManager setUbiquitous:YES itemAtURL:currentPlace destinationURL:icloudURL error:&error];
The system takes care of everything else - i.e. just accessing the file normally with
residentData = [[NSMutableArray alloc] initWithContentsOfURL:icloudURL];
Has worked.
I have A NSdata value that it needs to be converted into a string.
So far my app works fine loading a QC composition from a server however, I have a warning when I tell QC to load data from server.
It loads the file just fine but is there a way to avoid this worming?
I have tried to convert data to string using
NSString* newStr = [[NSString alloc] initWithData:urlData
encoding:NSUTF8StringEncoding];
HOwever, it is giving a null location of the file
Find the way I was loading the file with wrong method the correct one is:
QCComposition *qc = [QCComposition compositionWithData:urlData];
I have a program that retrieves data from a link and i write it out to the Log like this.
NSURL *getURL=[NSURL URLWithString:#"link.php"];
NSError *error=nil;
NSString *str=[NSString stringWithContentsofURL:getURL encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%",str);
This prints to the log the three values from my php as expected.
However I am having a little difficulty saving this in an array which then displays it those values in a UISplitviewController (the leftcontroller side).
which is written like this
showArray=[[NSMutableArray alloc]initWithContentofURL:getURL];
then in cellForRowAtIndexPath: method is
cell.textLabel.text=[showArray object atIndex:indexPath.row];
A second thing i have tried is write myURL to an array and tried to initlize showArray with ContentsofArray like this
NSArray *retults=[NSArray arraywithContentsOFURL:getURL];
showArray=[[NSArray alloc]initWithArray:retults];
but THAT dont work
BUT if i say
showArray=[[NSMutableArray alloc]initWithObjects:#"One",#"Two",nil];
One and two shows in my leftview controller....
Would love is someone could help me with this...Thank you
Are you trying to add the contents of the URL or the URL itself ?
If you are trying to just add the URL, then use :
showArray = [#[getURL] mutableCopy];
However, if you are trying to add the contents of the URL, then the doc clearly states that the URL must represent a string representation of an array.
Furthermore :
Returns nil if the location can’t be opened or if the contents of the location can’t be parsed into an array.
EDIT :
I saw your comment on your post and your data looks like JSON data.
You should take a look at the NSJSONSerialisation class which is pretty straightforward to use (you'll find lots of example here on SO).
U have done web services perfectly, now wat u have to do is parse it to an array
First download the SBJSON files in this link
https://github.com/stig/json-framework/
Then, copy them to your workspace.
Then, in the viewController add this
#import "SBJson.h"
Your JSON data contains values in the form of dictionary
SO, to parse them
SBJsonParser * parser=[SBJsonParser new];
NSDictionary * jsonData=(NSDictionary *)[parser objectWithString:outputData];
NSArray * arr=(NSArray *)[NSDictionary objectForKey:#"animal"];
I think this will help
I've followed this tutorial: http://www.iphonedevcentral.com/hello-uitableview/
I've got it working great and all, but I can't quite figure out how to make it work by accessing the plist they've included, online.
Their plist is located here: http://www.iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist
And the source code for their tutorial is here: http://www.iphonedevcentral.com/wp-content/uploads/2009/07/MyDVDLibrary01.zip
For what i can see, the call to the file on that exercise refer to an internal resource:
libraryContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:libraryPlist ofType:#"plist"]];
In order to have tis working you need to download the TestData.plist file from the server and copy it in your Xcode project. If you want to load the file from the server you have to change those lines with something like this:
libraryContent = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist"]];