I've done this with HTML, like so:
NSString *theweatherhtml = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"*URL STRING WOULD GO HERE*"]];
But when i do this with an XML file, it returns a 'Program received signal: “EXC_BAD_ACCESS”.'
Is there a different way to do this for XML files?
That method is deprecated. You need to use:
initWithContentsOfURL:encoding:error:
http://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSString_Class/Reference/NSString.html
You can use NSMutableDictionary using objectforkey Or NSArray and then convert each value into related varibles. Like -
int value;
value = [[savedStock objectForKey:#"userID"] intValue];
Related
2012-10-12 19:29:43
Aquivalent NSDateFormatter:
[_dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Throws an exception ... Why?
Thank you!
Reference: http://waracle.net/mobile/iphone-nsdateformatter-date-formatting-table/
UPDATE:
The problem seems to be the string. If I hardcode the string:
NSString * string = #"2012-10-12 19:29:43";
It works fine.
I read it from an array of key-value pairs so I do:
NSString * string = [NSString stringWithFormat:#"%#", (NSString *)[[NSArray readFromPlistFile:#"latestchangesdates"] valueForKey:#"newsLastEdited"]];
Console Output:
#1:
2012-10-12 10:16:49
#2:
( "2012-10-12 10:16:49" )
I think the problem is something which has to do with the string parse from the array.
UPDATE 2:
[[[NSArray readFromPlistFile:#"latestchangesdates"] objectAtIndex:0] valueForKey:#"newsLastEdited"]]
... finally did it.
It looks like the key newsLastEdited in your Plist file is actually returning an array not a string.
The line:
NSString *string = [NSString stringWithFormat:#"%#", (NSString *)[[NSArray readFromPlistFile:#"latestchangesdates"] valueForKey:#"newsLastEdited"]];
is simply casting to an NSString the returned value from [[NSArray readFromPlistFile:#"latestchangesdates"] valueForKey:#"newsLastEdited"]. This does not automatically make the returned value a string.
What happens if you use the following instead:
NSString *string = [[[NSArray readFromPlistFile:#"latestchangesdates"] valueForKey:#"newsLastEdited"] objectAtIndex:0];
If I'm correct, this will take the first element of the array returned from the Plist newsLastEdited key. If this works then you should probably take some time to understand the data structure stored in the Plist file.
[[[NSArray readFromPlistFile:#"latestchangesdates"] objectAtIndex:0] valueForKey:#"newsLastEdited"]]
... finally did it.
I got problem in following statement.
NSArray *feedsData = [strFeedsResponse JSONValue];
I presume strFeedsResponse is an instance of NSString. There is no such method as JSONValue in NSString. You need NSString category and add the JSONValue method there.
You can use for example SBJson library https://github.com/stig/json-framework/, which contains NSString+SBJson.h header, which adds the JSONValue method for NSString.
This header must be than imported in the source file where you want to use the JSONValue method:
#import NSString+SBJSON.h
More about categories for example here: http://macdevelopertips.com/objective-c/objective-c-categories.html
One of two things is happening:
strFeedsResponse is not ACTUALLY an instance of an NSString. Maybe it is null or it has been initiated with an incorrect value. You can add a breakpoint to your code to check the value that is stored in strFeedsResponse before you call JSONValue on it.
You have not correctly imported the JSON framework that you are using into your class. You need to add the JSON header to your class.
Hope this helps future searchers on this error...
The reason for the error is that the current version of SBJson seems to not contain the NSString category "NSString+SBJSON.h" referred to by an earlier poster. So, you could easily write your own, or just use the SBJsonParser class directly ("address" is just an NSString containing something like "24 Elm Street, Yourtown, New Mexico"):
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:#"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%#", esc_addr];
NSString *response = [NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *googleResponse = [parser objectWithString:response];
Cheers!
If you are using a webservice, then give the URL for that.
try this way...
NSArray* latestentry = (NSDictionary*)[responseString JSONValue];
NSArray *feedsData = [[strFeedsResponse JSONValue] mutableCopy];
Use this Line, it will be useful to you..
This error generally comes when method not found.Here it comes because JSONValue function is not found.
Please make sure you have included json header files where you calling this function.
I am having my server send me a list of all the images in a certain users folder, it sends me a string and i am using SBJsonparser to get it. here is the coding:
NSString *url = [[NSString alloc]initWithFormat:#"http://mysite.com/members/grabimages.php?&username=%#", [getLogin objectAtIndex:0]];
NSString *connect = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:url]];
SBJsonParser *parser = [[SBJsonParser alloc]init];
NSString *imagenames = [[NSString alloc]init];
imagenames = [parser objectWithString:connect error:nil];
NSLog(#"%#", imagenames);
The string returns this:
(
"./username/default/img1.jpg",
"./username/default/img2.jpg"
)
There is going to be lots of URL's in this one string at some point, i want to be able to separate all the URL's and then add each one of them into a new string.
How would i be able to do this?
Thanks :)
the error from the other post error says the object was an NSMutableArray. presumably, that object was imagenames.
if so, then you declare it like so:
NSArray *imagenames = [parser objectWithString:connect error:nil];
then you use your relative base url to compose the url, as they are relative to some directory you must know of. so, you would compose the base (as an NSURL), and use one of the -[NSURL URLByAppending... methods to create new URLs from the elements of imagenames.
You could use NSArray *array = [imagenames componentsSeparatedByString:#","]; to separate the the urls
using iphone sdk 4.0. The callback for an http request gives data as an NSData object
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the data received to our data
[theData appendData:data];
}
In my php script on the server i am returning an array as follows
var_dump($array).
How do i get my array back from the NSMutableData object 'theData' obove on my iphone.
Thanks
You have a string describing your array (or maybe several arrays?) stored as a sequence of bytes in your NSMutableData object. In order to turn it back into an array you're going to need to parse the var_dump output, which is likely to be arduous.
If you can find a library (or roll your own code) to return your data in Apple plist format, your task will be much easier: you can use
[NSPropertyListSerialization propertyListFromData:mutabilityOption:format:errorDescription:]
which takes an NSData (or NSMutableData) pointer as its first argument. Try http://code.google.com/p/cfpropertylist/ for a starting point.
From the example code at the cfpropertylist page:
$plist = new CFPropertyList();
$td = new CFTypeDetector();
$guessedStructure = $td->toCFType( $array );
$plist->add( $guessedStructure );
// and then return the plist content with
$plist->toXML()
and in your iOS code:
NSString *errorString = nil;
NSArray *array = [[NSPropertyListSerialization
propertyListFromData:theData
mutabilityOption:NSPropertyListImmutable
format:nil
errorDescription:&errorString] retain];
I would likely use YAJL on iOS, and $var = json_encode($array); in the PHP. Then in the iOS, I would parse that content from the NSData input like:
YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:YAJLParserOptionsAllowComments | YAJLParserOptionsCheckUTF8];
parser.delegate = [[[MyArrayParserDelegate alloc] init] autorelease];
[parser parse:data];
NSArray *thePhpArrayReceived = parser.delegate.resultantArray;
Please check out how to structure the delegate, and get YAJL here : Get YAJL + Readme
PHP outputs text so you will have to read that NSData as NSString and then parse out the array data according to the format specified by var_dump. As a starting point, the following code snippet should print out the array (as text) to your console:
NSString * dump = [[NSString alloc] initWithData:theData
encoding:NSUTF8StringEncoding];
NSLog(#"%#", dump);
[dump release];
As Seamus Campbell points out, there are better ways of doing this. Another option would be to output XML from your PHP script, and then use Cocoa's XML parsing methods to retreive the array.
i have an NSString that is value in plist format. i download this string from url. but i dont wanna write it to a file. when the string comes Asynchronous, i want to put it to nsmutablearray.
how can i convert string (in plist format) to nsmutablearray?
there is some methods, initWithContentsOfURL, initWithContentsOfFile. but no intiwithstring.
this method works Synchronous:
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfURL:url];
There is a -propertyList method in NSString.
NSMutableArray* tmpArray = [[theString propertyList] mutableCopy];
...
[tmpArray release];
Note that this method will throw an exception (i.e. throw) if the string is not in plist format. To have a better error checking, try to download the data as NSData, and use the NSPropertyListSerialization methods.