I have the following problem:
in header;
GDataXMLDocument *doc;
NSString *xmlBody;
#property (nonatomic,copy) NSString *xmlBody;
#property (nonatomic,retain) GDataXMLDocument *doc;
in m
#import "tchLoader.h"
#import "Variable.h"
#import "DisplayVariable.h"
#implementation tchLoader
#synthesize responseXMLData,lastLoadedResponseXMLData;
#synthesize conn;
#synthesize doc;
#synthesize xmlBody;
- (void)loadXML:(id<tchLoaderDelegate>)delegate {
NSString *theBaseXML= #"some xml code here"
if (self.xmlBody==nil){
self.xmlBody=theBaseXML;
}
_delegate = delegate;
/*
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [#"www.alues.com" UTF8String]);
SCNetworkReachabilityGetFlags(reachability, &flags);
// The reachability flags are a bitwise set of flags that contain the information about
// connection availability
BOOL reachable = ! (flags & kSCNetworkReachabilityFlagsConnectionRequired);
*/
NSString *soapMessage =self.xmlBody;
NSURL *url = [NSURL URLWithString:#"https://area.tch-values.com/soapmwp/mws"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[request addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue: #"http://www.tch-values.com/webservice" forHTTPHeaderField:#"SOAPAction"];
[request addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
if ([NSURLConnection canHandleRequest:request] && true) {
self.conn = [[NSURLConnection alloc ]initWithRequest:request delegate:self];
if (self.conn) {
self.responseXMLData = [NSMutableData data];
}
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"ERROR with theConenction");
[self.doc release];
[self.conn release];
[self.responseXMLData release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(#"DONE. Received Bytes: %d", [self.responseXMLData length]);
//[self.conn release];
if ([_delegate respondsToSelector:#selector(xmlDidFinishLoading)]) {
[_delegate xmlDidFinishLoading];
}
}
-(void)insertAnswers: (NSMutableArray*) answeredVariables{
for (Variable * variable in answeredVariables)
{
NSInteger pageID=[variable pageId];
//NSMutableArray *answers=[NSMutableArray arrayWithCapacity:[[variable variableValues] count]];
NSString *path = [NSString stringWithFormat:#"//inferenceresponse/state/variable[pageId=%d]/valuedefinition",pageID];
NSArray *valueElement = [doc nodesForXPath:path error:nil];
GDataXMLElement *valueDefinitionElement;
if (valueElement.count > 0) {
valueDefinitionElement= (GDataXMLElement *) [valueElement objectAtIndex:0];
}
GDataXMLElement * sourceElement = [GDataXMLNode elementWithName:#"source"];
[sourceElement addAttribute:[GDataXMLNode attributeWithName:#"type" stringValue:#"ask user"]];
GDataXMLElement * timeStampElement = [GDataXMLNode elementWithName:#"timestamp" stringValue:#"12345"];
[sourceElement addChild:timeStampElement];
GDataXMLElement * assignmentElement = [GDataXMLNode elementWithName:#"assignmentnumber" stringValue:#"6"];
for(NSString *answer in variable.variableValues){
GDataXMLElement * variableValueElement = [GDataXMLNode elementWithName:#"variablevalue"];
[variableValueElement addAttribute:[GDataXMLNode attributeWithName:#"value" stringValue:answer]];
[valueDefinitionElement addChild:variableValueElement];
}
[valueDefinitionElement addChild:sourceElement];
[valueDefinitionElement addChild:assignmentElement];
}
NSData *xmlData = self.doc.XMLData;
NSString *theXML = [[NSString alloc] initWithBytes:[xmlData bytes] length:[xmlData length] encoding:NSUTF8StringEncoding];
theXML =[theXML stringByReplacingOccurrencesOfString:#"inferenceresponse" withString:#"inferencerequest"];
theXML =[theXML stringByReplacingOccurrencesOfString:#"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" withString:#"<SOAP-ENV:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> "];
theXML =[theXML stringByReplacingOccurrencesOfString:#"xmlns=\"\"" withString:#"xmlns=\"http://www.tch-values.com/xml/webservice\""];
theXML =[theXML stringByReplacingOccurrencesOfString:#"<state goalreached=\"false\">" withString:#"<state goalreached=\"false\"> <value>PlanKB</value> <goalvariable>myGoal</goalvariable> "];
self.xmlBody=theXML;
[theXML release];
//[self.doc release];
NSLog(self.xmlBody);
}
- (NSMutableArray*)variablesForPageID:(NSString*)pageID {
NSMutableArray *variables = nil;
if (self.responseXMLData) {
variables = [NSMutableArray arrayWithCapacity:10];
NSData *xmlData = self.responseXMLData;
NSError *error;
self.doc=nil;
doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
if (self.doc == nil) {
return nil;
}
NSArray *status = [doc nodesForXPath:#"//inferenceresponse/state[#goalreached='true']" error:nil];
if([status count]==1){
self.xmlBody=nil;
Variable *variable=[[Variable alloc] init];
NSString *path = [NSString stringWithFormat:#"//inferenceresponse/state/displayvariables/display[#isDisplayShown='false']"];
NSArray *displayVariablesElements = [doc nodesForXPath:path error:nil];
NSMutableArray *disps=[[NSMutableArray alloc] init];
if(displayVariablesElements.count >0){
for(GDataXMLElement *disElement in displayVariablesElements){
DisplayVariable *disVar=[[DisplayVariable alloc] init];
NSArray *disPageid = [disElement nodesForXPath:#"#displayPageId" error:nil];
GDataXMLElement *Pageid = (GDataXMLElement *) [disPageid objectAtIndex:0];
disVar.displayPageId =Pageid.stringValue;
NSArray *disName = [disElement nodesForXPath:#"displayname" error:nil];
if(disName.count >0){
GDataXMLElement *disNam = (GDataXMLElement *) [disName objectAtIndex:0];
disVar.displayName =disNam.stringValue;
}
NSArray *disValue = [disElement nodesForXPath:#"displayvalue" error:nil];
if(disValue.count >0){
GDataXMLElement *disVal = (GDataXMLElement *) [disValue objectAtIndex:0];
disVar.displayValue =disVal.stringValue;
}
NSArray *disId = [disElement nodesForXPath:#"#id" error:nil];
GDataXMLElement *disIdEl = (GDataXMLElement *) [disId objectAtIndex:0];
disVar.pageId =[disIdEl.stringValue intValue];
[disps addObject:disVar];
[disVar release];
}
variable.displayVariables=disps;
[disps release];
}
variable.lastVariableofConsultation=YES;
[variables addObject:variable];
[variable release];
}
else{
NSArray *inferenceMembers = [doc nodesForXPath:#"//inferenceresponse/state/variable[not(valuedefinition/variablevalue)]" error:nil];
for (GDataXMLElement *variableElement in inferenceMembers) {
Variable *variable=[[Variable alloc] init];
NSArray *items = [variableElement nodesForXPath:#"domaindefinition/domain/enumType/domainitem" error:nil];
NSMutableArray *domainItems = [NSMutableArray arrayWithCapacity:items.count];
for (int i=0; i<items.count;i++) {
GDataXMLElement *domainItem = (GDataXMLElement *) [items objectAtIndex:i];
[domainItems addObject:domainItem.stringValue];
}
variable.domainItems=domainItems;
NSArray *names = [variableElement nodesForXPath:#"name/#name" error:nil];
if (names.count > 0) {
GDataXMLElement *nameElement = (GDataXMLElement *) [names objectAtIndex:0];
variable.variableName = nameElement.stringValue;
}
NSArray *pageId = [variableElement nodesForXPath:#"pageId" error:nil];
}
}
return variables;
}
- (void)dealloc {
[responseXMLData release] ;
[lastLoadedResponseXMLData release] ;
[conn release];
[doc release];
[xmlBody release];
[super dealloc];
}
#end
#import "tchLoader.h"
#import "Variable.h"
#import "DisplayVariable.h"
#implementation tchLoader
#synthesize responseXMLData,lastLoadedResponseXMLData;
#synthesize conn;
#synthesize doc;
#synthesize xmlBody;
If I [theXML release] it crashes, If I dont release theXML then it works perfect but I see memory leak in simulator Instruments tool. (There are also lots of memory leaks I see in this code but couldnt figure out by instrumnets tool what is going on)
This looks wrong:
[doc release];
Why are you releasing an object managed by a property?
In your #synthesize statements change them to:
#synthesize doc = doc_;
#synthesize xmlBody = xmlBody_;
Then fix all the resulting errors to go through the property, releasing the properties ONLY in dealloc.
Edit:
You say it crashes releasing theXML. This code is wrong:
NSString *theXML = [[NSString alloc] initWithBytes:[xmlData bytes] length:[xmlData length] encoding:NSUTF8StringEncoding];
theXML =[theXML stringByReplacingOccurrencesOfString:#"inferenceresponse" withString:#"inferencerequest"];
[theXML release];
You alloc a string, replace that variable with the "stringByReplacing..." call with a string that is autoreleased, then try to release the resulting string that is autoreleased which will crash. When you don't need to keep a string around after the method you are in, ALWAYS use autorelease. The correct code is:
NSString *theXML = [[[NSString alloc] initWithBytes:[xmlData bytes] length:[xmlData length] encoding:NSUTF8StringEncoding] autorelease];
theXML =[theXML stringByReplacingOccurrencesOfString:#"inferenceresponse" withString:#"inferencerequest"];
And take out [theXML release] - there will be no leak.
I think you should really switch to using ARC as soon as you possibly can... it will save you from a lot of such misunderstandings.
If you want to release an ivar/property(retain), this is not how to to it:
[self.doc release];
instead:
self.doc = nil;
One "problem" with singleton objects is that they appear to leak. If you create an object and never destroy it, Instruments says it's a leak, even if your intention was never to release it.
NSData *xmlData = doc.XMLData;
newXML = [[NSString alloc] initWithBytes:[xmlData bytes] length:[xmlData length]
encoding:NSUTF8StringEncoding];
What is your newXML? is it a ivar and synthesize it and retain it? if yes the compiler will automatically generate getter and setter methods.
NSData *xmlData = doc.XMLData;
self.newXML = [[NSString alloc] initWithBytes:[xmlData bytes] length:[xmlData length]
encoding:NSUTF8StringEncoding];
self.timestamp = nil;
If its not property then
NSString* newXML = [[NSString alloc] initWithBytes:[xmlData bytes] length:[xmlData length]
encoding:NSUTF8StringEncoding];
self.xmlBody=newXML;
[newXML release];
Related
I am trying to load web content asynchronously. I have a large amount of web calls in my viewdidappear method and my app is very unresponsive. I understand the concepts of synchronous and asynchronous loading of content, but don't know how to tell if this is being done asynchronously. The code below is simply embedded in my viewdidappear method, and I assume it is loading synchronously. How would I edit this to make it load asynchronously? Thank you all!
NSString *strURLtwo = [NSString stringWithFormat:#"http://website.com/json.php?
id=%#&lat1=%#&lon1=%#",id, lat, lon];
NSData *dataURLtwo = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURLtwo]];
NSArray *readJsonArray = [NSJSONSerialization JSONObjectWithData:dataURLtwo options:0
error:nil];
NSDictionary *element1 = [readJsonArray objectAtIndex:0];
NSString *name = [element1 objectForKey:#"name"];
NSString *address = [element1 objectForKey:#"address"];
NSString *phone = [element1 objectForKey:#"phone"];
You can use NSURLConnectionDelegate:
// Your public fetch method
-(void)fetchData
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://website.com/json.php?id=%#&lat1=%#&lon1=%#",id, lat, lon]];
// Put that URL into an NSURLRequest
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
// Create a connection that will exchange this request for data from the URL
connection = [[NSURLConnection alloc] initWithRequest:req
delegate:self
startImmediately:YES];
}
Implement the delegate methods:
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
{
// Add the incoming chunk of data to the container we are keeping
// The data always comes in the correct order
[jsonData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
// All data is downloaded. Do your stuff with the data
NSArray *readJsonArray = [NSJSONSerialization jsonData options:0 error:nil];
NSDictionary *element1 = [readJsonArray objectAtIndex:0];
NSString *name = [element1 objectForKey:#"name"];
NSString *address = [element1 objectForKey:#"address"];
NSString *phone = [element1 objectForKey:#"phone"];
jsonData = nil;
connection = nil;
}
// Show AlertView if error
- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
{
connection = nil;
jsonData = nil;
NSString *errorString = [NSString stringWithFormat:#"Fetch failed: %#", [error localizedDescription]];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error" message:errorString delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
}
For asynchronous web content loading, I recommend you to use AFNetworking . It'll solve lots of your major headache of networking in future. How to do:
1) subclass AFHTTPCLient, for example:
//WebClientHelper.h
#import "AFHTTPClient.h"
#interface WebClientHelper : AFHTTPClient{
}
+(WebClientHelper *)sharedClient;
#end
//WebClientHelper.m
#import "WebClientHelper.h"
#import "AFHTTPRequestOperation.h"
NSString *const gWebBaseURL = #"http://whateverBaseURL.com/";
#implementation WebClientHelper
+(WebClientHelper *)sharedClient
{
static WebClientHelper * _sharedClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:gWebBaseURL]];
});
return _sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url
{
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self registerHTTPOperationClass:[AFHTTPRequestOperation class]];
return self;
}
#end
2) Request asynchronously your web content, put this code in any relevant part
NSString *testNewsURL = #"http://whatever.com";
NSURL *url = [NSURL URLWithString:testNewsURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operationHttp =
[[WebClientHelper sharedClient] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSString *szResponse = [[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding] autorelease];
NSLog(#"Response: %#", szResponse );
//PUT your code here
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"Operation Error: %#", error.localizedDescription);
}];
[[WebClientHelper sharedClient] enqueueHTTPRequestOperation:operationHttp];
I am doing JSON parsing and I want to show my parsed data in a UITableView.
For that, I am trying to assign parsed data from NSMutableDictionary to NSArray to show in the table view but the array returns null.
Here my array returns null value;
NSMutableDictionary *tempDict1;
NSArray *arr = [[tempDict1 valueForKey:#"rates"] componentsSeparatedByString:#";"];
code
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
// NSArray *latestrates = [[responseString JSONValue] objectForKey:#"rates"];
[responseString release];
values = [responseString JSONValue];
array = [[NSMutableArray alloc] init];
array = [values valueForKey:#"rates"];
NSLog(#"array values:--> %#",array);
tempDict1 = (NSMutableDictionary *)array;
arr = [[tempDict1 valueForKey:#"rates"] componentsSeparatedByString:#";"];
NSString *subStar = #"=";
NSMutableArray *arrTitle = [[NSMutableArray alloc] init];
NSMutableArray *arrValues = [[NSMutableArray alloc] init];
[arrTitle removeAllObjects];
[arrValues removeAllObjects];
for (int i=0; i<[arr count]-1; i++)
{
[arrTitle addObject:[[arr objectAtIndex:i] substringToIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])-1]];
[arrValues addObject:[[arr objectAtIndex:i] substringFromIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])]];
NSLog(#"arrTitle is:--> %#",arrTitle);
}
tempDict1 = (NSMutableDictionary*)[array objectAtIndex:0];
array = [values valueForKey:#"rates"];
NSLog(#"tempDict--%#",tempDict1);
[arr retain];
[tbl_withData reloadData];
}
Try editing fourth line in connectionDidFinishLoading to
values = [responseString JSONFragments];
NSError *error = nil;
NSArray *array = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSLog(#"Your data - %#",array);
Now you can get it according to data format.
EDIT
I think you also dont know how to get a webResponse.
So here is a way to get webResponse -
First set XML delegate in your ViewController.h class
and declare a NSMutableData globaly
#interface ViewController : UIViewController<NSXMLParserDelegate>
#property(nonatomic, retain)NSMutableData *responseData;
#end
Now synthesized this responseData in your ViewController.m class
#synthesize responseData = _responseData;
Now you can send request on server in viewDidLoad: method its up to you in which method you want to send it.
-(void)viewDidLoad
{
NSString *urlString = [NSString stringWithFormat:#"http://EnterYourURLHere"];
NSURL *URL = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]init];
[urlRequest setURL:URL];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-type"];
NSURLConnection *urlConnection = [[NSURLConnection alloc]initWithRequest:urlRequest delegate:self];
if(!urlConnection)
{
[[[UIAlertView alloc]initWithTitle:#"OOoopppssS !!" message:#"There is an error occured. Please check your internet connection or try again." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show];
}
}
#pragma mark - Parsing delegate methods
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.responseData = [[NSMutableData alloc]init];
[self.responseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.responseData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//Now parse your data here -
NSError *error = nil;
NSArray *array = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableContainers error:&error];
NSLog(#"Your data - %#",array);
}
im new with objective-c. I have problem with memory leaking when developing iphone app. Leaking utility in Xcode shows that leaking problem with 'combArr'->'results' object. There is my function which parsing json from url and returns NSArray:
- (NSArray *)getListing2:(NSString *)item
from:(int)country {
//sending post request with some params
NSString *post = [#"product=" stringByAppendingString:item];
NSString *countryStr = [NSString stringWithFormat:#"&country=%d", country];
post = [post stringByAppendingString:countryStr];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *url = [prefs objectForKey:#"urlToApi"];
url = [url stringByAppendingString:#"/get-items/"];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
[request release];
//receiving json
NSString *jsonString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
SBJsonParser *json = [[SBJsonParser alloc] init];
NSError *error = nil;
//parsing json to nsdictionary
NSDictionary *results = [[NSDictionary alloc] initWithDictionary:[json objectWithString:jsonString error:&error]];
[json release];
[jsonString release];
//generate array of items
NSMutableArray *listOfItems = [[NSMutableArray alloc] init];
for (int i = 0; i < [[results objectForKey:#"data"] count]; i++) {
[listOfItems addObject:[[results objectForKey:#"data"] objectForKey:[NSString stringWithFormat:#"%d", i]]];
}
//saving items array and count info object into one array
NSArray * returnArr = [[[NSArray arrayWithObjects:listOfItems, [results valueForKey:#"count_info"], nil] retain] autorelease];
[listOfItems release];
[results release];
return returnArr;
}
And i executing this function here:
myApi *itemsApi = [[myApi alloc] init];
NSArray *combArr = [[izdApi getListing2:item from:countryId] retain];
[itemsApi release];
listOfItems = [[combArr objectAtIndex:0] retain];
if([listOfItems count] > 0){
priceArr = [[combArr objectAtIndex:1] retain];
}
else{
totalCount = 0;
}
[combArr release];
Thank you for helping
Every time you allocate memory, you must release it. (alloc, copy, retain).
You are releasing myApi, not itemsApi. Try this...
myApi *itemsApi = [[itemsApi alloc] init];
NSArray *combArr = [[izdApi getListing2:item from:countryId] retain];
[itemsApi release];
listOfItems = [[combArr objectAtIndex:0] retain];
if([listOfItems count] > 0){
priceArr = [[combArr objectAtIndex:1] retain];
}
else{
totalCount = 0;
}
[combArr release];
If you are using Xcode 4, Try turning on ARC. In short, ARC handles the releasing of all memory. A little burden off your shoulders and one less thing for you to worry about.
I have parsed JSON data the format of my JSON data is
http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=350&counties-selected=Vest-Agder,Aust-Agder
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.krsconnect.no/community/api.html?method=categories&appid=620&mainonly=true"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *object = [parser objectWithString:json_string error:nil];
NSArray *results = [parser objectWithString:json_string error:nil];
How about creating a data modal?
#interface book:NSObject {
NSString *catId;
NSString *bookName
}
create properties for these two instance vars.
#end
#implementation book
#synthesize catId,bookName;
- (id)init {
self = [super init];
}
- (id)initWithDictionary:(NSDictionary) dict {
self.catId = [dict valueForKey:#"categoryId"];
self.bookName = [dict valueForKey:#"name"];
}
- (void)dealloc {
[catId release];
[bookName release];
[super dealloc];
}
#end
and use it like this
NSMutableArray *bookArray = [[NSmutableArray alloc] initWithCapacity:0];
NSArray *results = [parser objectWithString:json_string error:nil];
for (int i=0; i<[results count]; i++) {
book *bookObject = [[book alloc] initWithDictionary:[results objectAtIndex:i]];
[bookArray addObject:bookObject];
[bookObject release];
}
I think you can do this by way
Adding dictionary to array.
for (NSDictionary *dict in mydict) {
[myArray addObject:dict];
}
You can do more modification and put the logic to set the values in array according
to key as well.
Hope this may help you.Well I haven't checked it now.But may get you to the solution.
Cheers.....
I have a UITableView that gets populated my a NSMutableArray. This is how I have it set up in my .h
#interface processViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *processList;
}
#property (copy, readwrite) NSMutableArray *processList;
and my .m
#synthesize processList;
-(void)viewDidLoad {
processList = [[NSMutableArray alloc] init];
}
I put a NSLog on it on the viewDidLoad, and it displays just fine. But after I run a action, the processList array returns null. Any ideas why?
ThanksCoulton
EDIT 1:
- (void)startUploads {
// Start UIActivity in the top
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// Start Pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Display results for testing purposes (commented out)
NSArray *resultstwo = [database executeQuery:#"SELECT * FROM processes"];
for (NSDictionary *rowtwo in resultstwo) {
// Get ID
int getUserIDcount = 0;
NSArray *getUserIDInfo = [database executeQuery:#"SELECT * FROM login"];
for (NSDictionary *getUserIDRow in getUserIDInfo) {
getUserIDcount++;
NSString *oneUserID = [getUserIDRow valueForKey:#"id"];
theUserID = [NSString stringWithFormat:#"%#", oneUserID];
}
// If theUserID exists...
if (getUserIDcount == 0) {
//myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector: #selector(checkLogin) userInfo: nil repeats: NO];
} else {
// Get URL of image
NSString *sqlImageUploadPathOne = #"./../Documents/";
NSString *sqlImageUploadPathTwo = [rowtwo valueForKey:#"image"];
NSString *getAlbumID = [rowtwo valueForKey:#"album"];
NSString *sqlImageUploadPath = [NSString stringWithFormat:#"%#%#",sqlImageUploadPathOne,sqlImageUploadPathTwo];
//testLabel.text = #"Uploading...";
// Display Image in UIImageView (uploadImageHidden)
UIImage *attemptImage = [UIImage imageNamed:sqlImageUploadPath];
[uploadImageHidden setImage:attemptImage];
// Upload to server
NSData *imageData = UIImageJPEGRepresentation(uploadImageHidden.image, 90);
NSString *urlStringOne = #"http://myflashpics.com/iphone_processes/upload.php?album=";
NSString *urlStringTwo = #"&id=";
NSString *urlString = [NSString stringWithFormat:#"%#%#%#%#",urlStringOne,getAlbumID,urlStringTwo,theUserID];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//NSLog(#"%#", returnString);
if ([returnString rangeOfString:#"yes"].location == NSNotFound) {
// Fail
} else {
// Delete image if successful
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *myFilePath = [documentsDirectoryPath stringByAppendingPathComponent:sqlImageUploadPathTwo];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:myFilePath error:NULL];
[database executeNonQuery:#"DELETE FROM processes WHERE image=?", sqlImageUploadPathTwo];
// Get Photo ID
NSArray *myWords = [returnString componentsSeparatedByString:#" "];
NSString *photoID = [myWords objectAtIndex:1];
NSString *usernameID = [myWords objectAtIndex:2];
NSString *defaultName = #"Photo uploaded from the flashpics iPhone application";
// Get Thumbnail URL
NSString *thumbnailURLOne = #"http://myflashpics.com/users/";
NSString *thumbnailURLTwo = #"/pictures/thumbnails/";
NSString *thumbnailURLThree = #".jpg";
NSString *thumbnailURL = [NSString stringWithFormat:#"%#%#%#%#%#",thumbnailURLOne,usernameID,thumbnailURLTwo,photoID,thumbnailURLThree];
// Download thumbnail
//NSLog(#"Downloading...");
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:thumbnailURL]]];
//NSLog(#"%f,%f",image.size.width,image.size.height);
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//NSLog(#"%#",docDir);
//NSLog(#"saving jpeg");
NSString *jpegFilePath = [NSString stringWithFormat:#"%#/%#_thumbnail.jpg",docDir,photoID];
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.2f)];//1.0f = 100% quality
[data2 writeToFile:jpegFilePath atomically:YES];
//NSLog(#"saving image done");
[image release];
// Put in database
NSString *thumbnailEnd = #"_thumbnail.jpg";
NSString *thumbnailLocation = [NSString stringWithFormat:#"%#%#",photoID,thumbnailEnd];
int theCount = 0;
NSArray *getUserIDInfotoo = [database executeQuery:#"SELECT * FROM images WHERE id=?",photoID];
for (NSDictionary *getUserIDRowtoo in getUserIDInfotoo) {
theCount++;
}
if (theCount == 0) {
[database executeNonQuery:#"INSERT INTO images (id, name, thumbnail, album) VALUES (?, ?, ?, ?)", photoID, defaultName, thumbnailLocation, getAlbumID];
}
//[NSThread detachNewThreadSelector:#selector(updateImages) toTarget:RootViewController withObject:nil];
//myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector: #selector(updateImages) userInfo: nil repeats: NO];
}
[request release];
[returnString release];
}
//NSLog([rowtwo valueForKey:#"image"]);
//NSLog([rowtwo valueForKey:#"album"]);
}
[pool release];
// Stop the UIActivity in the top bar
TableViewAppDelegate *dataCeter = (TableViewAppDelegate *)[[UIApplication sharedApplication] delegate];
if ([dataCeter.dataTen isEqualToString:#""]) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}
Edit 2:
Where the startUploads gets called (different .m)
processViewController *processTable = [[processViewController alloc] initWithNibName:#"processView.xib" bundle:nil];
[processTable startUploads];
[processTable release];
More code would be helpful but here are some suggestions:
Check if startUploads is being called before the view is loaded. The view will only be loaded when it is accessed for the first time to be added to a superview.
Consider initializing processList in your init method instead of viewDidLoad both to solve #1 and b/c the view can loaded & unloaded by iOS independently of the lifecycle of the viewController (depending on what other views you are displaying and whether any memory warnings occur).
Make sure you are releasing processList in dealloc. You only need to release it in viewDidUnload if it is recreated and loaded in viewDidLoad.
Your code sample doesn't show when startUploads is being called and you aren't adding any items to processList so it's hard to tell if the above is relevant. Post some more code and I'll revise my answer accordingly.
good luck!
[EDIT: added example code]
The code fragments you posted are not a complete implementation of a view controller and the associated objects that interact with it. Given the code I have seen, your application design does not conform to MVC (Model/View/Controller) design pattern and I would be doing things a bit differently. However, I don't want to make assumptions about code I haven't seen or your ultimate intent or ability as a developer. I can't write your app for you, just trying to directly help you with the specific question you asked regarding why your NSMutableArray property remains null after the startUploads action completed. With that in mind, here are my edits to the code you posted:
processViewController.m - add the following:
- (id)initWithNibNamed:(NSString *)nibName bundle:(NSBundle *)bundle {
self = [super initWithNibNamed:nibName bundle:bundle];
if (self) {
processList = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc {
self.processList = nil;
[super dealloc];
}
different.m
- (void)displayProcessVC {
ProcessViewController *processVC = [[ProcessViewController alloc] initWithNibNamed:#"processView.xib" bundle:nil];
NSLog(#"Different:displayProcessVC BEFORE STARTING UPLOAD, processList = %#", processVC.processList);
[processVC startUploads];
NSLog(#"Different:displayProcessVC AFTER STARTING UPLOAD, processList = %#", processVC.processList);
// would normally present process VC here
[processVC release];
}
Hope this helps.