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

有帮助吗?

解决方案 2

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

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top