0

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

bhavya kothari
  • 7,471
  • 4
  • 25
  • 53
Sleep Paralysis
  • 449
  • 5
  • 21

2 Answers2

1

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).

McNight
  • 2,571
  • 1
  • 16
  • 17
  • the doc says that string and array objects are accepted, my data returns as an array. so shouldnt my array be able to be initalized with the contents of said URL? – Sleep Paralysis Feb 19 '14 at 14:03
  • Yes it should. However, it is commonly used in combination with the method 'writeToURL:atomically:'. So your array pointed by the URL is surely not recognized by 'initWithContentsOfURL:'. – McNight Feb 19 '14 at 14:12
0

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

Chandru
  • 227
  • 2
  • 18
  • where you have outputData i used str since since i had written the json data too that var. – Sleep Paralysis Feb 19 '14 at 14:39
  • [{"animal":"Monkey"},{"animal":"Snake"},{"animal":"Cow"}], ehere it is stored – Chandru Feb 19 '14 at 14:44
  • I think im missing files, symbol(s) not found for architecture i386, i dragged everythig from the class folder to my project and did the import – Sleep Paralysis Feb 19 '14 at 14:47
  • Where is the variable JsonData used? also im getting a second warning on NSDictinary on 3rd line – Sleep Paralysis Feb 19 '14 at 14:49
  • did u added the .m files in build phases-> compile sources – Chandru Feb 19 '14 at 14:50
  • I had only added one, obviously i needed all, It runs but with a warning cause JsonData isnt used and another waring on NSDictionary ObjectForKey:@"animal"]; objectForKey isnt a method for that class.tried using dictionaryWithObjectsAndKeys:@"animal"nil but it also errored out – Sleep Paralysis Feb 19 '14 at 15:16
  • fine, did u got the concept how it worked, or else should i explain u – Chandru Feb 20 '14 at 05:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47937/discussion-between-sleep-paralysis-and-chandru) – Sleep Paralysis Feb 20 '14 at 11:30
  • I get it, I also formatted my php and program so now I am getting animal: {animal = monkey;} saved in The array, but so far nothing in my cell :D – Sleep Paralysis Feb 20 '14 at 11:34